Changeset 541dbc09 for libcfa/src/rational.cfa
- Timestamp:
- Jun 6, 2023, 8:44:14 AM (15 months ago)
- Branches:
- ast-experimental, master
- Children:
- 77afbb4
- Parents:
- 6a93e4d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/rational.cfa
r6a93e4d r541dbc09 10 10 // Created On : Wed Apr 6 17:54:28 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Aug 25 18:09:58 202213 // Update Count : 19 412 // Last Modified On : Mon Jun 5 22:49:06 2023 13 // Update Count : 196 14 14 // 15 15 … … 20 20 #pragma GCC visibility push(default) 21 21 22 forall( T | Arithmetic( T ) ) {22 forall( T | arithmetic( T ) ) { 23 23 // helper routines 24 24 … … 39 39 abort | "Invalid rational number construction: denominator cannot be equal to 0."; 40 40 } // exit 41 if ( d < (T){0} ) { d = -d; n = -n; } 41 if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator 42 42 return gcd( abs( n ), d ); // simplify 43 } // Rationalnumber::simplify43 } // simplify 44 44 45 45 // constructors 46 46 47 void ?{}( Rational(T) & r, zero_t ) {47 void ?{}( rational(T) & r, zero_t ) { 48 48 r{ (T){0}, (T){1} }; 49 49 } // rational 50 50 51 void ?{}( Rational(T) & r, one_t ) {51 void ?{}( rational(T) & r, one_t ) { 52 52 r{ (T){1}, (T){1} }; 53 53 } // rational 54 54 55 void ?{}( Rational(T) & r ) {55 void ?{}( rational(T) & r ) { 56 56 r{ (T){0}, (T){1} }; 57 57 } // rational 58 58 59 void ?{}( Rational(T) & r, T n ) {59 void ?{}( rational(T) & r, T n ) { 60 60 r{ n, (T){1} }; 61 61 } // rational 62 62 63 void ?{}( Rational(T) & r, T n, T d ) {64 T t = simplify( n, d ); // simplify63 void ?{}( rational(T) & r, T n, T d ) { 64 T t = simplify( n, d ); // simplify 65 65 r.[numerator, denominator] = [n / t, d / t]; 66 66 } // rational … … 68 68 // getter for numerator/denominator 69 69 70 T numerator( Rational(T) r ) {70 T numerator( rational(T) r ) { 71 71 return r.numerator; 72 72 } // numerator 73 73 74 T denominator( Rational(T) r ) {74 T denominator( rational(T) r ) { 75 75 return r.denominator; 76 76 } // denominator 77 77 78 [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ) {78 [ T, T ] ?=?( & [ T, T ] dest, rational(T) src ) { 79 79 return dest = src.[ numerator, denominator ]; 80 80 } // ?=? … … 82 82 // setter for numerator/denominator 83 83 84 T numerator( Rational(T) r, T n ) {84 T numerator( rational(T) r, T n ) { 85 85 T prev = r.numerator; 86 T t = gcd( abs( n ), r.denominator ); 86 T t = gcd( abs( n ), r.denominator ); // simplify 87 87 r.[numerator, denominator] = [n / t, r.denominator / t]; 88 88 return prev; 89 89 } // numerator 90 90 91 T denominator( Rational(T) r, T d ) {91 T denominator( rational(T) r, T d ) { 92 92 T prev = r.denominator; 93 T t = simplify( r.numerator, d ); // simplify93 T t = simplify( r.numerator, d ); // simplify 94 94 r.[numerator, denominator] = [r.numerator / t, d / t]; 95 95 return prev; … … 98 98 // comparison 99 99 100 int ?==?( Rational(T) l, Rational(T) r ) {100 int ?==?( rational(T) l, rational(T) r ) { 101 101 return l.numerator * r.denominator == l.denominator * r.numerator; 102 102 } // ?==? 103 103 104 int ?!=?( Rational(T) l, Rational(T) r ) {104 int ?!=?( rational(T) l, rational(T) r ) { 105 105 return ! ( l == r ); 106 106 } // ?!=? 107 107 108 int ?!=?( Rational(T) l, zero_t ) {109 return ! ( l == ( Rational(T)){ 0 } );108 int ?!=?( rational(T) l, zero_t ) { 109 return ! ( l == (rational(T)){ 0 } ); 110 110 } // ?!=? 111 111 112 int ?<?( Rational(T) l, Rational(T) r ) {112 int ?<?( rational(T) l, rational(T) r ) { 113 113 return l.numerator * r.denominator < l.denominator * r.numerator; 114 114 } // ?<? 115 115 116 int ?<=?( Rational(T) l, Rational(T) r ) {116 int ?<=?( rational(T) l, rational(T) r ) { 117 117 return l.numerator * r.denominator <= l.denominator * r.numerator; 118 118 } // ?<=? 119 119 120 int ?>?( Rational(T) l, Rational(T) r ) {120 int ?>?( rational(T) l, rational(T) r ) { 121 121 return ! ( l <= r ); 122 122 } // ?>? 123 123 124 int ?>=?( Rational(T) l, Rational(T) r ) {124 int ?>=?( rational(T) l, rational(T) r ) { 125 125 return ! ( l < r ); 126 126 } // ?>=? … … 128 128 // arithmetic 129 129 130 Rational(T) +?( Rational(T) r ) {131 return ( Rational(T)){ r.numerator, r.denominator };130 rational(T) +?( rational(T) r ) { 131 return (rational(T)){ r.numerator, r.denominator }; 132 132 } // +? 133 133 134 Rational(T) -?( Rational(T) r ) {135 return ( Rational(T)){ -r.numerator, r.denominator };134 rational(T) -?( rational(T) r ) { 135 return (rational(T)){ -r.numerator, r.denominator }; 136 136 } // -? 137 137 138 Rational(T) ?+?( Rational(T) l, Rational(T) r ) {138 rational(T) ?+?( rational(T) l, rational(T) r ) { 139 139 if ( l.denominator == r.denominator ) { // special case 140 return ( Rational(T)){ l.numerator + r.numerator, l.denominator };140 return (rational(T)){ l.numerator + r.numerator, l.denominator }; 141 141 } else { 142 return ( Rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };142 return (rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; 143 143 } // if 144 144 } // ?+? 145 145 146 Rational(T) ?+=?( Rational(T) & l, Rational(T) r ) {146 rational(T) ?+=?( rational(T) & l, rational(T) r ) { 147 147 l = l + r; 148 148 return l; 149 149 } // ?+? 150 150 151 Rational(T) ?+=?( Rational(T) & l, one_t ) {152 l = l + ( Rational(T)){ 1 };151 rational(T) ?+=?( rational(T) & l, one_t ) { 152 l = l + (rational(T)){ 1 }; 153 153 return l; 154 154 } // ?+? 155 155 156 Rational(T) ?-?( Rational(T) l, Rational(T) r ) {156 rational(T) ?-?( rational(T) l, rational(T) r ) { 157 157 if ( l.denominator == r.denominator ) { // special case 158 return ( Rational(T)){ l.numerator - r.numerator, l.denominator };158 return (rational(T)){ l.numerator - r.numerator, l.denominator }; 159 159 } else { 160 return ( Rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };160 return (rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; 161 161 } // if 162 162 } // ?-? 163 163 164 Rational(T) ?-=?( Rational(T) & l, Rational(T) r ) {164 rational(T) ?-=?( rational(T) & l, rational(T) r ) { 165 165 l = l - r; 166 166 return l; 167 167 } // ?-? 168 168 169 Rational(T) ?-=?( Rational(T) & l, one_t ) {170 l = l - ( Rational(T)){ 1 };169 rational(T) ?-=?( rational(T) & l, one_t ) { 170 l = l - (rational(T)){ 1 }; 171 171 return l; 172 172 } // ?-? 173 173 174 Rational(T) ?*?( Rational(T) l, Rational(T) r ) {175 return ( Rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator };174 rational(T) ?*?( rational(T) l, rational(T) r ) { 175 return (rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; 176 176 } // ?*? 177 177 178 Rational(T) ?*=?( Rational(T) & l, Rational(T) r ) {178 rational(T) ?*=?( rational(T) & l, rational(T) r ) { 179 179 return l = l * r; 180 180 } // ?*? 181 181 182 Rational(T) ?/?( Rational(T) l, Rational(T) r ) {182 rational(T) ?/?( rational(T) l, rational(T) r ) { 183 183 if ( r.numerator < (T){0} ) { 184 184 r.[numerator, denominator] = [-r.numerator, -r.denominator]; 185 185 } // if 186 return ( Rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator };186 return (rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator }; 187 187 } // ?/? 188 188 189 Rational(T) ?/=?( Rational(T) & l, Rational(T) r ) {189 rational(T) ?/=?( rational(T) & l, rational(T) r ) { 190 190 return l = l / r; 191 191 } // ?/? … … 194 194 195 195 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) 196 istype & ?|?( istype & is, Rational(T) & r ) {196 istype & ?|?( istype & is, rational(T) & r ) { 197 197 is | r.numerator | r.denominator; 198 198 T t = simplify( r.numerator, r.denominator ); … … 203 203 204 204 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { 205 ostype & ?|?( ostype & os, Rational(T) r ) {205 ostype & ?|?( ostype & os, rational(T) r ) { 206 206 return os | r.numerator | '/' | r.denominator; 207 207 } // ?|? 208 208 209 void ?|?( ostype & os, Rational(T) r ) {209 void ?|?( ostype & os, rational(T) r ) { 210 210 (ostype &)(os | r); ends( os ); 211 211 } // ?|? … … 213 213 } // distribution 214 214 215 forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) {216 Rational(T) ?\?( Rational(T) x, long int y ) {215 forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { 216 rational(T) ?\?( rational(T) x, long int y ) { 217 217 if ( y < 0 ) { 218 return ( Rational(T)){ x.denominator \ -y, x.numerator \ -y };218 return (rational(T)){ x.denominator \ -y, x.numerator \ -y }; 219 219 } else { 220 return ( Rational(T)){ x.numerator \ y, x.denominator \ y };220 return (rational(T)){ x.numerator \ y, x.denominator \ y }; 221 221 } // if 222 222 } // ?\? 223 223 224 Rational(T) ?\=?( Rational(T) & x, long int y ) {224 rational(T) ?\=?( rational(T) & x, long int y ) { 225 225 return x = x \ y; 226 226 } // ?\? … … 229 229 // conversion 230 230 231 forall( T | Arithmetic( T ) | { double convert( T ); } )232 double widen( Rational(T) r ) {231 forall( T | arithmetic( T ) | { double convert( T ); } ) 232 double widen( rational(T) r ) { 233 233 return convert( r.numerator ) / convert( r.denominator ); 234 234 } // widen 235 235 236 forall( T | Arithmetic( T ) | { double convert( T ); T convert( double ); } )237 Rational(T) narrow( double f, T md ) {236 forall( T | arithmetic( T ) | { double convert( T ); T convert( double ); } ) 237 rational(T) narrow( double f, T md ) { 238 238 // http://www.ics.uci.edu/~eppstein/numth/frap.c 239 if ( md <= (T){1} ) { // maximum fractional digits too small?240 return ( Rational(T)){ convert( f ), (T){1}};// truncate fraction239 if ( md <= (T){1} ) { // maximum fractional digits too small? 240 return (rational(T)){ convert( f ), (T){1}}; // truncate fraction 241 241 } // if 242 242 … … 260 260 if ( f > (double)0x7FFFFFFF ) break; // representation failure 261 261 } // for 262 return ( Rational(T)){ m00, m10 };262 return (rational(T)){ m00, m10 }; 263 263 } // narrow 264 264
Note: See TracChangeset
for help on using the changeset viewer.