Changes in / [6acd020:3d7d407]
- Files:
-
- 1 deleted
- 24 edited
-
doc/theses/andrew_beach_MMath/code/cond-catch.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/cond-catch.cpp (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/cond-fixup.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/cross-catch.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/cross-catch.cpp (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/cross-finally.cfa (modified) (1 diff)
-
doc/theses/andrew_beach_MMath/code/cross-resume.cfa (modified) (1 diff)
-
doc/theses/andrew_beach_MMath/code/resume-detor.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/resume-empty.cfa (modified) (1 diff)
-
doc/theses/andrew_beach_MMath/code/resume-finally.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/resume-other.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/throw-detor.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/throw-detor.cpp (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/throw-empty.cfa (modified) (1 diff)
-
doc/theses/andrew_beach_MMath/code/throw-empty.cpp (modified) (1 diff)
-
doc/theses/andrew_beach_MMath/code/throw-finally.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/throw-other.cfa (modified) (2 diffs)
-
doc/theses/andrew_beach_MMath/code/throw-other.cpp (modified) (2 diffs)
-
libcfa/prelude/builtins.c (modified) (2 diffs)
-
libcfa/src/Makefile.am (modified) (2 diffs)
-
libcfa/src/math.trait.hfa (deleted)
-
libcfa/src/rational.cfa (modified) (10 diffs)
-
libcfa/src/rational.hfa (modified) (2 diffs)
-
tests/.expect/rational.txt (modified) (2 diffs)
-
tests/rational.cfa (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/code/cond-catch.cfa
r6acd020 r3d7d407 19 19 throw_exception(); 20 20 } catch (empty_exception * exc ; should_catch) { 21 asm volatile ("# catch block (conditional)");21 // ... 22 22 } 23 23 } … … 37 37 cond_catch(); 38 38 } catch (empty_exception * exc) { 39 asm volatile ("# catch block (unconditional)");39 // ... 40 40 } 41 41 } -
doc/theses/andrew_beach_MMath/code/cond-catch.cpp
r6acd020 r3d7d407 22 22 throw; 23 23 } 24 asm volatile ("# catch block (conditional)");25 24 } 26 25 } … … 40 39 cond_catch(); 41 40 } catch (EmptyException &) { 42 asm volatile ("# catch block (unconditional)");41 // ... 43 42 } 44 43 } -
doc/theses/andrew_beach_MMath/code/cond-fixup.cfa
r6acd020 r3d7d407 19 19 throw_exception(); 20 20 } catchResume (empty_exception * exc ; should_catch) { 21 asm volatile ("# fixup block (conditional)");21 // ... 22 22 } 23 23 } … … 37 37 cond_catch(); 38 38 } catchResume (empty_exception * exc) { 39 asm volatile ("# fixup block (unconditional)");39 // ... 40 40 } 41 41 } -
doc/theses/andrew_beach_MMath/code/cross-catch.cfa
r6acd020 r3d7d407 7 7 EHM_EXCEPTION(not_raised_exception)(); 8 8 9 EHM_VIRTUAL_TABLE(not_raised_exception, not_vt);10 11 9 int main(int argc, char * argv[]) { 12 10 unsigned int times = 1; 13 bool should_throw = false;11 unsigned int total_frames = 1; 14 12 if (1 < argc) { 15 13 times = strtol(argv[1], 0p, 10); 14 } 15 if (2 < argc) { 16 total_frames = strtol(argv[2], 0p, 10); 16 17 } 17 18 … … 19 20 for (unsigned int count = 0 ; count < times ; ++count) { 20 21 try { 21 asm volatile ("# try block" : "=rm" (should_throw)); 22 if (should_throw) { 23 throw (not_raised_exception){¬_vt}; 24 } 22 // ... 25 23 } catch (not_raised_exception *) { 26 asm volatile ("# catch block");24 // ... 27 25 } 28 26 } -
doc/theses/andrew_beach_MMath/code/cross-catch.cpp
r6acd020 r3d7d407 11 11 int main(int argc, char * argv[]) { 12 12 unsigned int times = 1; 13 bool should_throw = false;14 13 if (1 < argc) { 15 14 times = strtol(argv[1], nullptr, 10); … … 19 18 for (unsigned int count = 0 ; count < times ; ++count) { 20 19 try { 21 asm volatile ("# try block" : "=rm" (should_throw)); 22 if (should_throw) { 23 throw NotRaisedException(); 24 } 20 // ... 25 21 } catch (NotRaisedException &) { 26 asm volatile ("# catch block");22 // ... 27 23 } 28 24 } -
doc/theses/andrew_beach_MMath/code/cross-finally.cfa
r6acd020 r3d7d407 5 5 #include <stdlib.hfa> 6 6 7 EHM_EXCEPTION(not_raised_exception)();8 9 EHM_VIRTUAL_TABLE(not_raised_exception, not_vt);10 11 7 int main(int argc, char * argv[]) { 12 8 unsigned int times = 1; 13 bool should_throw = false;9 unsigned int total_frames = 1; 14 10 if (1 < argc) { 15 11 times = strtol(argv[1], 0p, 10); 12 } 13 if (2 < argc) { 14 total_frames = strtol(argv[2], 0p, 10); 16 15 } 17 16 18 17 Time start_time = timeHiRes(); 19 18 for (unsigned int count = 0 ; count < times ; ++count) { 20 try { 21 asm volatile ("# try block" : "=rm" (should_throw)); 22 if (should_throw) { 23 throw (not_raised_exception){¬_vt}; 24 } 19 try { 20 // ... 25 21 } finally { 26 asm volatile ("# finally block");22 // ... 27 23 } 28 24 } -
doc/theses/andrew_beach_MMath/code/cross-resume.cfa
r6acd020 r3d7d407 20 20 for (unsigned int count = 0 ; count < times ; ++count) { 21 21 try { 22 asm volatile ("");22 // ... 23 23 } catchResume (not_raised_exception *) { 24 asm volatile ("");24 // ... 25 25 } 26 26 } -
doc/theses/andrew_beach_MMath/code/resume-detor.cfa
r6acd020 r3d7d407 12 12 13 13 void ^?{}(WithDestructor & this) { 14 asm volatile ("# destructor body"); 14 // ... 15 15 } 16 16 17 17 void unwind_destructor(unsigned int frames) { 18 if (frames) {18 if (frames) { 19 19 20 WithDestructor object;21 unwind_destructor(frames - 1);22 } else {23 throwResume (empty_exception){&empty_vt};24 }20 WithDestructor object; 21 unwind_destructor(frames - 1); 22 } else { 23 throwResume (empty_exception){&empty_vt}; 24 } 25 25 } 26 26 … … 36 36 37 37 Time start_time = timeHiRes(); 38 for (int count = 0 ; count < times ; ++count) {39 try {40 unwind_destructor(total_frames);41 } catchResume (empty_exception *) {42 asm volatile ("# fixup block"); 43 }44 }38 for (int count = 0 ; count < times ; ++count) { 39 try { 40 unwind_destructor(total_frames); 41 } catchResume (empty_exception *) { 42 // ... 43 } 44 } 45 45 Time end_time = timeHiRes(); 46 46 sout | "Run-Time (ns): " | (end_time - start_time)`ns; -
doc/theses/andrew_beach_MMath/code/resume-empty.cfa
r6acd020 r3d7d407 32 32 unwind_empty(total_frames); 33 33 } catchResume (empty_exception *) { 34 asm volatile ("# fixup block");34 // ... 35 35 } 36 36 } -
doc/theses/andrew_beach_MMath/code/resume-finally.cfa
r6acd020 r3d7d407 14 14 unwind_finally(frames - 1); 15 15 } finally { 16 asm volatile ("# finally block");16 // ... 17 17 } 18 18 } else { … … 36 36 unwind_finally(total_frames); 37 37 } catchResume (empty_exception *) { 38 asm volatile ("# fixup block");38 // ... 39 39 } 40 40 } -
doc/theses/andrew_beach_MMath/code/resume-other.cfa
r6acd020 r3d7d407 16 16 unwind_other(frames - 1); 17 17 } catchResume (not_raised_exception *) { 18 asm volatile ("# fixup block (stack)");18 // ... 19 19 } 20 20 } else { … … 38 38 unwind_other(total_frames); 39 39 } catchResume (empty_exception *) { 40 asm volatile ("# fixup block (base)");40 // ... 41 41 } 42 42 } -
doc/theses/andrew_beach_MMath/code/throw-detor.cfa
r6acd020 r3d7d407 12 12 13 13 void ^?{}(WithDestructor & this) { 14 asm volatile ("# destructor body");14 // ... 15 15 } 16 16 … … 39 39 unwind_destructor(total_frames); 40 40 } catch (empty_exception *) { 41 asm volatile ("# catch block");41 // ... 42 42 } 43 43 } -
doc/theses/andrew_beach_MMath/code/throw-detor.cpp
r6acd020 r3d7d407 10 10 11 11 struct WithDestructor { 12 ~WithDestructor() { 13 asm volatile ("# destructor body"); 14 } 12 ~WithDestructor() {} 15 13 }; 16 14 … … 39 37 unwind_destructor(total_frames); 40 38 } catch (EmptyException &) { 41 asm volatile ("# catch block");39 // ... 42 40 } 43 41 } -
doc/theses/andrew_beach_MMath/code/throw-empty.cfa
r6acd020 r3d7d407 32 32 unwind_empty(total_frames); 33 33 } catch (empty_exception *) { 34 asm volatile ("# catch block");34 // ... 35 35 } 36 36 } -
doc/theses/andrew_beach_MMath/code/throw-empty.cpp
r6acd020 r3d7d407 32 32 unwind_empty(total_frames); 33 33 } catch (EmptyException &) { 34 asm volatile ("# catch block");34 // ... 35 35 } 36 36 } -
doc/theses/andrew_beach_MMath/code/throw-finally.cfa
r6acd020 r3d7d407 14 14 unwind_finally(frames - 1); 15 15 } finally { 16 asm volatile ("# finally block");16 // ... 17 17 } 18 18 } else { … … 36 36 unwind_finally(total_frames); 37 37 } catch (empty_exception *) { 38 asm volatile ("# catch block");38 // ... 39 39 } 40 40 } -
doc/theses/andrew_beach_MMath/code/throw-other.cfa
r6acd020 r3d7d407 16 16 unwind_other(frames - 1); 17 17 } catch (not_raised_exception *) { 18 asm volatile ("# catch block (stack)");18 // ... 19 19 } 20 20 } else { … … 38 38 unwind_other(total_frames); 39 39 } catch (empty_exception *) { 40 asm volatile ("# catch block (base)");40 // ... 41 41 } 42 42 } -
doc/theses/andrew_beach_MMath/code/throw-other.cpp
r6acd020 r3d7d407 16 16 unwind_other(frames - 1); 17 17 } catch (NotRaisedException &) { 18 asm volatile ("# catch block (stack)");18 // ... 19 19 } 20 20 } else { … … 38 38 unwind_other(total_frames); 39 39 } catch (EmptyException &) { 40 asm volatile ("# catch block (base)");40 // ... 41 41 } 42 42 } -
libcfa/prelude/builtins.c
r6acd020 r3d7d407 10 10 // Created On : Fri Jul 21 16:21:03 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Jul 20 17:31:40202113 // Update Count : 1 2812 // Last Modified On : Tue Apr 13 17:26:32 2021 13 // Update Count : 117 14 14 // 15 15 … … 78 78 79 79 static inline { 80 forall( T | { T ?+=?(T &, one_t ); } )81 T ++?(T & x ) { return x += 1; }80 forall( DT & | { DT & ?+=?( DT &, one_t ); } ) 81 DT & ++?( DT & x ) { return x += 1; } 82 82 83 forall( T | { T ?+=?(T &, one_t ); } )84 T ?++( T & x ) {T tmp = x; x += 1; return tmp; }83 forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?+=?( DT &, one_t ); } ) 84 DT & ?++( DT & x ) { DT tmp = x; x += 1; return tmp; } 85 85 86 forall( T | { T ?-=?(T &, one_t ); } )87 T --?(T & x ) { return x -= 1; }86 forall( DT & | { DT & ?-=?( DT &, one_t ); } ) 87 DT & --?( DT & x ) { return x -= 1; } 88 88 89 forall( T | { T ?-=?(T &, one_t ); } )90 T ?--( T & x ) {T tmp = x; x -= 1; return tmp; }89 forall( DT & | sized(DT) | { void ?{}( DT &, DT ); void ^?{}( DT & ); DT & ?-=?( DT &, one_t ); } ) 90 DT & ?--( DT & x ) { DT tmp = x; x -= 1; return tmp; } 91 91 92 forall( T | { int ?!=?( T, zero_t ); } )93 int !?( T & x ) { return !( x != 0 ); }92 forall( DT & | { int ?!=?( const DT &, zero_t ); } ) 93 int !?( const DT & x ) { return !( x != 0 ); } 94 94 } // distribution 95 95 -
libcfa/src/Makefile.am
r6acd020 r3d7d407 11 11 ## Created On : Sun May 31 08:54:01 2015 12 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Fri Jul 16 16:00:40202114 ## Update Count : 25 513 ## Last Modified On : Sat Apr 24 09:09:56 2021 14 ## Update Count : 254 15 15 ############################################################################### 16 16 … … 45 45 exception.h \ 46 46 gmp.hfa \ 47 math.trait.hfa \48 47 math.hfa \ 49 48 time_t.hfa \ -
libcfa/src/rational.cfa
r6acd020 r3d7d407 10 10 // Created On : Wed Apr 6 17:54:28 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Jul 20 16:30:06 202113 // Update Count : 1 9312 // Last Modified On : Sat Feb 8 17:56:36 2020 13 // Update Count : 187 14 14 // 15 15 … … 18 18 #include "stdlib.hfa" 19 19 20 forall( T | Arithmetic( T) ) {20 forall( RationalImpl | arithmetic( RationalImpl ) ) { 21 21 // helper routines 22 22 23 23 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce 24 24 // rationals. alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm 25 static T gcd( T a, Tb ) {25 static RationalImpl gcd( RationalImpl a, RationalImpl b ) { 26 26 for ( ;; ) { // Euclid's algorithm 27 Tr = a % b;28 if ( r == ( T){0} ) break;27 RationalImpl r = a % b; 28 if ( r == (RationalImpl){0} ) break; 29 29 a = b; 30 30 b = r; … … 33 33 } // gcd 34 34 35 static T simplify( T & n, T& d ) {36 if ( d == ( T){0} ) {35 static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) { 36 if ( d == (RationalImpl){0} ) { 37 37 abort | "Invalid rational number construction: denominator cannot be equal to 0."; 38 38 } // exit 39 if ( d < ( T){0} ) { d = -d; n = -n; } // move sign to numerator39 if ( d < (RationalImpl){0} ) { d = -d; n = -n; } // move sign to numerator 40 40 return gcd( abs( n ), d ); // simplify 41 41 } // Rationalnumber::simplify … … 43 43 // constructors 44 44 45 void ?{}( Rational(T) & r, zero_t ) { 46 r{ (T){0}, (T){1} }; 47 } // rational 48 49 void ?{}( Rational(T) & r, one_t ) { 50 r{ (T){1}, (T){1} }; 51 } // rational 52 53 void ?{}( Rational(T) & r ) { 54 r{ (T){0}, (T){1} }; 55 } // rational 56 57 void ?{}( Rational(T) & r, T n ) { 58 r{ n, (T){1} }; 59 } // rational 60 61 void ?{}( Rational(T) & r, T n, T d ) { 62 T t = simplify( n, d ); // simplify 45 void ?{}( Rational(RationalImpl) & r ) { 46 r{ (RationalImpl){0}, (RationalImpl){1} }; 47 } // rational 48 49 void ?{}( Rational(RationalImpl) & r, RationalImpl n ) { 50 r{ n, (RationalImpl){1} }; 51 } // rational 52 53 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) { 54 RationalImpl t = simplify( n, d ); // simplify 63 55 r.[numerator, denominator] = [n / t, d / t]; 64 56 } // rational 65 57 58 void ?{}( Rational(RationalImpl) & r, zero_t ) { 59 r{ (RationalImpl){0}, (RationalImpl){1} }; 60 } // rational 61 62 void ?{}( Rational(RationalImpl) & r, one_t ) { 63 r{ (RationalImpl){1}, (RationalImpl){1} }; 64 } // rational 65 66 66 // getter for numerator/denominator 67 67 68 T numerator( Rational(T) r ) {68 RationalImpl numerator( Rational(RationalImpl) r ) { 69 69 return r.numerator; 70 70 } // numerator 71 71 72 T denominator( Rational(T) r ) {72 RationalImpl denominator( Rational(RationalImpl) r ) { 73 73 return r.denominator; 74 74 } // denominator 75 75 76 [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ) {76 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) { 77 77 return dest = src.[ numerator, denominator ]; 78 78 } // ?=? … … 80 80 // setter for numerator/denominator 81 81 82 T numerator( Rational(T) r, Tn ) {83 Tprev = r.numerator;84 Tt = gcd( abs( n ), r.denominator ); // simplify82 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) { 83 RationalImpl prev = r.numerator; 84 RationalImpl t = gcd( abs( n ), r.denominator ); // simplify 85 85 r.[numerator, denominator] = [n / t, r.denominator / t]; 86 86 return prev; 87 87 } // numerator 88 88 89 T denominator( Rational(T) r, Td ) {90 Tprev = r.denominator;91 Tt = simplify( r.numerator, d ); // simplify89 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) { 90 RationalImpl prev = r.denominator; 91 RationalImpl t = simplify( r.numerator, d ); // simplify 92 92 r.[numerator, denominator] = [r.numerator / t, d / t]; 93 93 return prev; … … 96 96 // comparison 97 97 98 int ?==?( Rational( T) l, Rational(T) r ) {98 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 99 99 return l.numerator * r.denominator == l.denominator * r.numerator; 100 100 } // ?==? 101 101 102 int ?!=?( Rational( T) l, Rational(T) r ) {102 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 103 103 return ! ( l == r ); 104 104 } // ?!=? 105 105 106 int ?!=?( Rational(T) l, zero_t ) { 107 return ! ( l == (Rational(T)){ 0 } ); 108 } // ?!=? 109 110 int ?<?( Rational(T) l, Rational(T) r ) { 106 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 111 107 return l.numerator * r.denominator < l.denominator * r.numerator; 112 108 } // ?<? 113 109 114 int ?<=?( Rational( T) l, Rational(T) r ) {110 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 115 111 return l.numerator * r.denominator <= l.denominator * r.numerator; 116 112 } // ?<=? 117 113 118 int ?>?( Rational( T) l, Rational(T) r ) {114 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 119 115 return ! ( l <= r ); 120 116 } // ?>? 121 117 122 int ?>=?( Rational( T) l, Rational(T) r ) {118 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 123 119 return ! ( l < r ); 124 120 } // ?>=? … … 126 122 // arithmetic 127 123 128 Rational( T) +?( Rational(T) r ) {129 return (Rational( T)){ r.numerator, r.denominator };124 Rational(RationalImpl) +?( Rational(RationalImpl) r ) { 125 return (Rational(RationalImpl)){ r.numerator, r.denominator }; 130 126 } // +? 131 127 132 Rational( T) -?( Rational(T) r ) {133 return (Rational( T)){ -r.numerator, r.denominator };128 Rational(RationalImpl) -?( Rational(RationalImpl) r ) { 129 return (Rational(RationalImpl)){ -r.numerator, r.denominator }; 134 130 } // -? 135 131 136 Rational( T) ?+?( Rational(T) l, Rational(T) r ) {132 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 137 133 if ( l.denominator == r.denominator ) { // special case 138 return (Rational( T)){ l.numerator + r.numerator, l.denominator };134 return (Rational(RationalImpl)){ l.numerator + r.numerator, l.denominator }; 139 135 } else { 140 return (Rational( T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };136 return (Rational(RationalImpl)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; 141 137 } // if 142 138 } // ?+? 143 139 144 Rational(T) ?+=?( Rational(T) & l, Rational(T) r ) { 145 l = l + r; 146 return l; 147 } // ?+? 148 149 Rational(T) ?+=?( Rational(T) & l, one_t ) { 150 l = l + (Rational(T)){ 1 }; 151 return l; 152 } // ?+? 153 154 Rational(T) ?-?( Rational(T) l, Rational(T) r ) { 140 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 155 141 if ( l.denominator == r.denominator ) { // special case 156 return (Rational( T)){ l.numerator - r.numerator, l.denominator };142 return (Rational(RationalImpl)){ l.numerator - r.numerator, l.denominator }; 157 143 } else { 158 return (Rational( T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };144 return (Rational(RationalImpl)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; 159 145 } // if 160 146 } // ?-? 161 147 162 Rational(T) ?-=?( Rational(T) & l, Rational(T) r ) { 163 l = l - r; 164 return l; 165 } // ?-? 166 167 Rational(T) ?-=?( Rational(T) & l, one_t ) { 168 l = l - (Rational(T)){ 1 }; 169 return l; 170 } // ?-? 171 172 Rational(T) ?*?( Rational(T) l, Rational(T) r ) { 173 return (Rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; 148 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 149 return (Rational(RationalImpl)){ l.numerator * r.numerator, l.denominator * r.denominator }; 174 150 } // ?*? 175 151 176 Rational(T) ?*=?( Rational(T) & l, Rational(T) r ) { 177 return l = l * r; 178 } // ?*? 179 180 Rational(T) ?/?( Rational(T) l, Rational(T) r ) { 181 if ( r.numerator < (T){0} ) { 152 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 153 if ( r.numerator < (RationalImpl){0} ) { 182 154 r.[numerator, denominator] = [-r.numerator, -r.denominator]; 183 155 } // if 184 return (Rational( T)){ l.numerator * r.denominator, l.denominator * r.numerator };156 return (Rational(RationalImpl)){ l.numerator * r.denominator, l.denominator * r.numerator }; 185 157 } // ?/? 186 158 187 Rational(T) ?/=?( Rational(T) & l, Rational(T) r ) {188 return l = l / r;189 } // ?/?190 191 159 // I/O 192 160 193 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T& ); } )194 istype & ?|?( istype & is, Rational( T) & r ) {161 forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } ) 162 istype & ?|?( istype & is, Rational(RationalImpl) & r ) { 195 163 is | r.numerator | r.denominator; 196 Tt = simplify( r.numerator, r.denominator );164 RationalImpl t = simplify( r.numerator, r.denominator ); 197 165 r.numerator /= t; 198 166 r.denominator /= t; … … 200 168 } // ?|? 201 169 202 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T); } ) {203 ostype & ?|?( ostype & os, Rational( T) r ) {170 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) { 171 ostype & ?|?( ostype & os, Rational(RationalImpl) r ) { 204 172 return os | r.numerator | '/' | r.denominator; 205 173 } // ?|? 206 174 207 void ?|?( ostype & os, Rational( T) r ) {175 void ?|?( ostype & os, Rational(RationalImpl) r ) { 208 176 (ostype &)(os | r); ends( os ); 209 177 } // ?|? … … 211 179 } // distribution 212 180 213 forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { 214 Rational(T) ?\?( Rational(T) x, long int y ) { 215 if ( y < 0 ) { 216 return (Rational(T)){ x.denominator \ -y, x.numerator \ -y }; 217 } else { 218 return (Rational(T)){ x.numerator \ y, x.denominator \ y }; 219 } // if 220 } // ?\? 221 222 Rational(T) ?\=?( Rational(T) & x, long int y ) { 223 return x = x \ y; 224 } // ?\? 225 } // distribution 181 forall( RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } ) 182 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) { 183 if ( y < 0 ) { 184 return (Rational(RationalImpl)){ x.denominator \ -y, x.numerator \ -y }; 185 } else { 186 return (Rational(RationalImpl)){ x.numerator \ y, x.denominator \ y }; 187 } // if 188 } 226 189 227 190 // conversion 228 191 229 forall( T | Arithmetic( T ) | { double convert( T); } )230 double widen( Rational( T) r ) {192 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } ) 193 double widen( Rational(RationalImpl) r ) { 231 194 return convert( r.numerator ) / convert( r.denominator ); 232 195 } // widen 233 196 234 forall( T | Arithmetic( T ) | { double convert( T ); Tconvert( double ); } )235 Rational( T) narrow( double f, Tmd ) {197 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } ) 198 Rational(RationalImpl) narrow( double f, RationalImpl md ) { 236 199 // http://www.ics.uci.edu/~eppstein/numth/frap.c 237 if ( md <= ( T){1} ) { // maximum fractional digits too small?238 return (Rational( T)){ convert( f ), (T){1}}; // truncate fraction200 if ( md <= (RationalImpl){1} ) { // maximum fractional digits too small? 201 return (Rational(RationalImpl)){ convert( f ), (RationalImpl){1}}; // truncate fraction 239 202 } // if 240 203 241 204 // continued fraction coefficients 242 Tm00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 };243 Tai, t;205 RationalImpl m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; 206 RationalImpl ai, t; 244 207 245 208 // find terms until denom gets too big … … 258 221 if ( f > (double)0x7FFFFFFF ) break; // representation failure 259 222 } // for 260 return (Rational( T)){ m00, m10 };223 return (Rational(RationalImpl)){ m00, m10 }; 261 224 } // narrow 262 225 -
libcfa/src/rational.hfa
r6acd020 r3d7d407 12 12 // Created On : Wed Apr 6 17:56:25 2016 13 13 // Last Modified By : Peter A. Buhr 14 // Last Modified On : Tue Jul 20 17:45:29 202115 // Update Count : 1 1814 // Last Modified On : Tue Mar 26 23:16:10 2019 15 // Update Count : 109 16 16 // 17 17 … … 19 19 20 20 #include "iostream.hfa" 21 #include "math.trait.hfa" // Arithmetic 21 22 trait scalar( T ) { 23 }; 24 25 trait arithmetic( T | scalar( T ) ) { 26 int !?( T ); 27 int ?==?( T, T ); 28 int ?!=?( T, T ); 29 int ?<?( T, T ); 30 int ?<=?( T, T ); 31 int ?>?( T, T ); 32 int ?>=?( T, T ); 33 void ?{}( T &, zero_t ); 34 void ?{}( T &, one_t ); 35 T +?( T ); 36 T -?( T ); 37 T ?+?( T, T ); 38 T ?-?( T, T ); 39 T ?*?( T, T ); 40 T ?/?( T, T ); 41 T ?%?( T, T ); 42 T ?/=?( T &, T ); 43 T abs( T ); 44 }; 22 45 23 46 // implementation 24 47 25 forall( T | Arithmetic( T) ) {48 forall( RationalImpl | arithmetic( RationalImpl ) ) { 26 49 struct Rational { 27 T numerator, denominator;// invariant: denominator > 050 RationalImpl numerator, denominator; // invariant: denominator > 0 28 51 }; // Rational 29 52 30 53 // constructors 31 54 32 void ?{}( Rational( T) & r );33 void ?{}( Rational( T) & r, zero_t);34 void ?{}( Rational( T) & r, one_t);35 void ?{}( Rational( T) & r, T n);36 void ?{}( Rational( T) & r, T n, T d);55 void ?{}( Rational(RationalImpl) & r ); 56 void ?{}( Rational(RationalImpl) & r, RationalImpl n ); 57 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ); 58 void ?{}( Rational(RationalImpl) & r, zero_t ); 59 void ?{}( Rational(RationalImpl) & r, one_t ); 37 60 38 61 // numerator/denominator getter 39 62 40 T numerator( Rational(T) r );41 T denominator( Rational(T) r );42 [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src );63 RationalImpl numerator( Rational(RationalImpl) r ); 64 RationalImpl denominator( Rational(RationalImpl) r ); 65 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ); 43 66 44 67 // numerator/denominator setter 45 68 46 T numerator( Rational(T) r, Tn );47 T denominator( Rational(T) r, Td );69 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ); 70 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ); 48 71 49 72 // comparison 50 73 51 int ?==?( Rational(T) l, Rational(T) r ); 52 int ?!=?( Rational(T) l, Rational(T) r ); 53 int ?!=?( Rational(T) l, zero_t ); // => ! 54 int ?<?( Rational(T) l, Rational(T) r ); 55 int ?<=?( Rational(T) l, Rational(T) r ); 56 int ?>?( Rational(T) l, Rational(T) r ); 57 int ?>=?( Rational(T) l, Rational(T) r ); 74 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 75 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 76 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 77 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 78 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 79 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 58 80 59 81 // arithmetic 60 82 61 Rational(T) +?( Rational(T) r ); 62 Rational(T) -?( Rational(T) r ); 63 Rational(T) ?+?( Rational(T) l, Rational(T) r ); 64 Rational(T) ?+=?( Rational(T) & l, Rational(T) r ); 65 Rational(T) ?+=?( Rational(T) & l, one_t ); // => ++?, ?++ 66 Rational(T) ?-?( Rational(T) l, Rational(T) r ); 67 Rational(T) ?-=?( Rational(T) & l, Rational(T) r ); 68 Rational(T) ?-=?( Rational(T) & l, one_t ); // => --?, ?-- 69 Rational(T) ?*?( Rational(T) l, Rational(T) r ); 70 Rational(T) ?*=?( Rational(T) & l, Rational(T) r ); 71 Rational(T) ?/?( Rational(T) l, Rational(T) r ); 72 Rational(T) ?/=?( Rational(T) & l, Rational(T) r ); 83 Rational(RationalImpl) +?( Rational(RationalImpl) r ); 84 Rational(RationalImpl) -?( Rational(RationalImpl) r ); 85 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 86 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 87 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 88 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 73 89 74 90 // I/O 75 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T& ); } )76 istype & ?|?( istype &, Rational( T) & );91 forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } ) 92 istype & ?|?( istype &, Rational(RationalImpl) & ); 77 93 78 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T); } ) {79 ostype & ?|?( ostype &, Rational( T) );80 void ?|?( ostype &, Rational( T) );94 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) { 95 ostype & ?|?( ostype &, Rational(RationalImpl) ); 96 void ?|?( ostype &, Rational(RationalImpl) ); 81 97 } // distribution 82 98 } // distribution 83 99 84 forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { 85 Rational(T) ?\?( Rational(T) x, long int y ); 86 Rational(T) ?\=?( Rational(T) & x, long int y ); 87 } // distribution 100 forall( RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} ) 101 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ); 88 102 89 103 // conversion 90 forall( T | Arithmetic( T ) | { double convert( T); } )91 double widen( Rational( T) r );92 forall( T | Arithmetic( T ) | { double convert( T ); Tconvert( double );} )93 Rational( T) narrow( double f, Tmd );104 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } ) 105 double widen( Rational(RationalImpl) r ); 106 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double );} ) 107 Rational(RationalImpl) narrow( double f, RationalImpl md ); 94 108 95 109 // Local Variables: // -
tests/.expect/rational.txt
r6acd020 r3d7d407 1 1 constructor 2 a : 3/1 b : 4/1 c : 0/1 d : 0/1 e : 1/1 3 a : 1/2 b : 5/7 4 a : 2/3 b : -3/2 5 a : -2/3 b : 3/2 6 7 comparison 8 a : -2/1 b : -3/2 9 a == 0 : 0 10 a == 1 : 0 11 a != 0 : 1 12 ! a : 0 13 a != b : 1 14 a < b : 1 15 a <= b : 1 16 a > b : 0 17 a >= b : 0 18 2 3/1 4/1 0/1 0/1 1/1 3 1/2 5/7 4 2/3 -3/2 5 -2/3 3/2 6 logical 7 -2/1 -3/2 8 1 9 1 10 1 11 0 12 0 19 13 arithmetic 20 a : -2/1 b : -3/2 21 a + b : -7/2 22 a += b : -7/2 23 ++a : -5/2 24 a++ : -5/2 25 a : -3/2 26 a - b : 0/1 27 a -= b : 0/1 28 --a : -1/1 29 a-- : -1/1 30 a : -2/1 31 a * b : 3/1 32 a / b : 4/3 33 a \ 2 : 4/1 b \ 2 : 9/4 34 a \ -2 : 1/4 b \ -2 : 4/9 35 14 -2/1 -3/2 15 -7/2 16 -1/2 17 3/1 18 4/3 36 19 conversion 37 20 0.75 … … 41 24 1/7 42 25 355/113 43 26 decompose 44 27 more tests 45 28 -3/2 -
tests/rational.cfa
r6acd020 r3d7d407 10 10 // Created On : Mon Mar 28 08:43:12 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue Jul 20 18:13:40 202113 // Update Count : 10712 // Last Modified On : Sat Feb 8 18:46:23 2020 13 // Update Count : 86 14 14 // 15 15 … … 26 26 sout | "constructor"; 27 27 RatInt a = { 3 }, b = { 4 }, c, d = 0, e = 1; 28 sout | "a : " | a | "b : " | b | "c : " | c | "d : " | d | "e : "| e;28 sout | a | b | c | d | e; 29 29 30 30 a = (RatInt){ 4, 8 }; 31 31 b = (RatInt){ 5, 7 }; 32 sout | "a : " | a | "b : "| b;32 sout | a | b; 33 33 a = (RatInt){ -2, -3 }; 34 34 b = (RatInt){ 3, -2 }; 35 sout | "a : " | a | "b : "| b;35 sout | a | b; 36 36 a = (RatInt){ -2, 3 }; 37 37 b = (RatInt){ 3, 2 }; 38 sout | "a : " | a | "b : " | b; 39 sout | nl; 38 sout | a | b; 40 39 41 sout | " comparison";40 sout | "logical"; 42 41 a = (RatInt){ -2 }; 43 42 b = (RatInt){ -3, 2 }; 44 sout | "a : " | a | "b : " | b; 45 sout | "a == 0 : " | a == (Rational(int)){0}; // FIX ME 46 sout | "a == 1 : " | a == (Rational(int)){1}; // FIX ME 47 sout | "a != 0 : " | a != 0; 48 sout | "! a : " | ! a; 49 sout | "a != b : " | a != b; 50 sout | "a < b : " | a < b; 51 sout | "a <= b : " | a <= b; 52 sout | "a > b : " | a > b; 53 sout | "a >= b : " | a >= b; 54 sout | nl; 43 sout | a | b; 44 // sout | a == 1; // FIX ME 45 sout | a != b; 46 sout | a < b; 47 sout | a <= b; 48 sout | a > b; 49 sout | a >= b; 55 50 56 51 sout | "arithmetic"; 57 sout | "a : " | a | "b : " | b; 58 sout | "a + b : " | a + b; 59 sout | "a += b : " | (a += b); 60 sout | "++a : " | ++a; 61 sout | "a++ : " | a++; 62 sout | "a : " | a; 63 sout | "a - b : " | a - b; 64 sout | "a -= b : " | (a -= b); 65 sout | "--a : " | --a; 66 sout | "a-- : " | a--; 67 sout | "a : " | a; 68 sout | "a * b : " | a * b; 69 sout | "a / b : " | a / b; 70 sout | "a \\ 2 : " | a \ 2u | "b \\ 2 : " | b \ 2u; 71 sout | "a \\ -2 : " | a \ -2 | "b \\ -2 : " | b \ -2; 72 sout | nl; 52 sout | a | b; 53 sout | a + b; 54 sout | a - b; 55 sout | a * b; 56 sout | a / b; 57 // sout | a \ 2 | b \ 2; // FIX ME 58 // sout | a \ -2 | b \ -2; 73 59 74 60 sout | "conversion"; … … 82 68 sout | narrow( 0.14285714285714, 16 ); 83 69 sout | narrow( 3.14159265358979, 256 ); 84 sout | nl;85 70 86 //sout | "decompose";87 //int n, d;88 //[n, d] = a;89 //sout | a | n | d;71 sout | "decompose"; 72 int n, d; 73 // [n, d] = a; 74 // sout | a | n | d; 90 75 91 76 sout | "more tests";
Note:
See TracChangeset
for help on using the changeset viewer.