[a493682] | 1 | // |
---|
[53ba273] | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
[a493682] | 6 | // |
---|
| 7 | // rational.c -- |
---|
| 8 | // |
---|
[53ba273] | 9 | // Author : Peter A. Buhr |
---|
| 10 | // Created On : Wed Apr 6 17:54:28 2016 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[f5e37a4] | 12 | // Last Modified On : Wed Nov 27 18:06:43 2024 |
---|
| 13 | // Update Count : 208 |
---|
[a493682] | 14 | // |
---|
[53ba273] | 15 | |
---|
[58b6d1b] | 16 | #include "rational.hfa" |
---|
| 17 | #include "fstream.hfa" |
---|
| 18 | #include "stdlib.hfa" |
---|
[53ba273] | 19 | |
---|
[0aa4beb] | 20 | #pragma GCC visibility push(default) |
---|
| 21 | |
---|
[f5e37a4] | 22 | // Arithmetic, Relational |
---|
| 23 | |
---|
[541dbc09] | 24 | forall( T | arithmetic( T ) ) { |
---|
[3ce0d440] | 25 | // helper routines |
---|
| 26 | |
---|
| 27 | // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce |
---|
| 28 | // rationals. alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm |
---|
[5dc4c7e] | 29 | static T gcd( T a, T b ) { |
---|
[f6a4917] | 30 | for () { // Euclid's algorithm |
---|
[5dc4c7e] | 31 | T r = a % b; |
---|
| 32 | if ( r == (T){0} ) break; |
---|
[3ce0d440] | 33 | a = b; |
---|
| 34 | b = r; |
---|
| 35 | } // for |
---|
| 36 | return b; |
---|
| 37 | } // gcd |
---|
| 38 | |
---|
[5dc4c7e] | 39 | static T simplify( T & n, T & d ) { |
---|
| 40 | if ( d == (T){0} ) { |
---|
[ff2a33e] | 41 | abort | "Invalid rational number construction: denominator cannot be equal to 0."; |
---|
[3ce0d440] | 42 | } // exit |
---|
[541dbc09] | 43 | if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator |
---|
[3ce0d440] | 44 | return gcd( abs( n ), d ); // simplify |
---|
[541dbc09] | 45 | } // simplify |
---|
[3ce0d440] | 46 | |
---|
| 47 | // constructors |
---|
| 48 | |
---|
[541dbc09] | 49 | void ?{}( rational(T) & r, zero_t ) { |
---|
[5dc4c7e] | 50 | r{ (T){0}, (T){1} }; |
---|
[3ce0d440] | 51 | } // rational |
---|
| 52 | |
---|
[541dbc09] | 53 | void ?{}( rational(T) & r, one_t ) { |
---|
[5dc4c7e] | 54 | r{ (T){1}, (T){1} }; |
---|
[3ce0d440] | 55 | } // rational |
---|
| 56 | |
---|
[541dbc09] | 57 | void ?{}( rational(T) & r ) { |
---|
[5dc4c7e] | 58 | r{ (T){0}, (T){1} }; |
---|
[3ce0d440] | 59 | } // rational |
---|
| 60 | |
---|
[541dbc09] | 61 | void ?{}( rational(T) & r, T n ) { |
---|
[5dc4c7e] | 62 | r{ n, (T){1} }; |
---|
[f00b2c2c] | 63 | } // rational |
---|
| 64 | |
---|
[541dbc09] | 65 | void ?{}( rational(T) & r, T n, T d ) { |
---|
| 66 | T t = simplify( n, d ); // simplify |
---|
[5dc4c7e] | 67 | r.[numerator, denominator] = [n / t, d / t]; |
---|
[f00b2c2c] | 68 | } // rational |
---|
[3ce0d440] | 69 | |
---|
| 70 | // getter for numerator/denominator |
---|
| 71 | |
---|
[541dbc09] | 72 | T numerator( rational(T) r ) { |
---|
[3ce0d440] | 73 | return r.numerator; |
---|
| 74 | } // numerator |
---|
| 75 | |
---|
[541dbc09] | 76 | T denominator( rational(T) r ) { |
---|
[3ce0d440] | 77 | return r.denominator; |
---|
| 78 | } // denominator |
---|
| 79 | |
---|
[92211d9] | 80 | [ T, T ] ?=?( & [ T, T ] dst, rational(T) src ) { |
---|
| 81 | return dst = src.[ numerator, denominator ]; |
---|
[3ce0d440] | 82 | } // ?=? |
---|
| 83 | |
---|
| 84 | // setter for numerator/denominator |
---|
| 85 | |
---|
[541dbc09] | 86 | T numerator( rational(T) r, T n ) { |
---|
[5dc4c7e] | 87 | T prev = r.numerator; |
---|
[541dbc09] | 88 | T t = gcd( abs( n ), r.denominator ); // simplify |
---|
[0087e0e] | 89 | r.[numerator, denominator] = [n / t, r.denominator / t]; |
---|
[3ce0d440] | 90 | return prev; |
---|
| 91 | } // numerator |
---|
| 92 | |
---|
[541dbc09] | 93 | T denominator( rational(T) r, T d ) { |
---|
[5dc4c7e] | 94 | T prev = r.denominator; |
---|
[541dbc09] | 95 | T t = simplify( r.numerator, d ); // simplify |
---|
[0087e0e] | 96 | r.[numerator, denominator] = [r.numerator / t, d / t]; |
---|
[3ce0d440] | 97 | return prev; |
---|
| 98 | } // denominator |
---|
| 99 | |
---|
| 100 | // comparison |
---|
| 101 | |
---|
[541dbc09] | 102 | int ?==?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 103 | return l.numerator * r.denominator == l.denominator * r.numerator; |
---|
| 104 | } // ?==? |
---|
| 105 | |
---|
[541dbc09] | 106 | int ?!=?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 107 | return ! ( l == r ); |
---|
| 108 | } // ?!=? |
---|
| 109 | |
---|
[541dbc09] | 110 | int ?!=?( rational(T) l, zero_t ) { |
---|
| 111 | return ! ( l == (rational(T)){ 0 } ); |
---|
[5dc4c7e] | 112 | } // ?!=? |
---|
| 113 | |
---|
[541dbc09] | 114 | int ?<?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 115 | return l.numerator * r.denominator < l.denominator * r.numerator; |
---|
| 116 | } // ?<? |
---|
| 117 | |
---|
[541dbc09] | 118 | int ?<=?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 119 | return l.numerator * r.denominator <= l.denominator * r.numerator; |
---|
| 120 | } // ?<=? |
---|
| 121 | |
---|
[541dbc09] | 122 | int ?>?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 123 | return ! ( l <= r ); |
---|
| 124 | } // ?>? |
---|
| 125 | |
---|
[541dbc09] | 126 | int ?>=?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 127 | return ! ( l < r ); |
---|
| 128 | } // ?>=? |
---|
| 129 | |
---|
| 130 | // arithmetic |
---|
| 131 | |
---|
[541dbc09] | 132 | rational(T) +?( rational(T) r ) { |
---|
| 133 | return (rational(T)){ r.numerator, r.denominator }; |
---|
[3ce0d440] | 134 | } // +? |
---|
[53ba273] | 135 | |
---|
[541dbc09] | 136 | rational(T) -?( rational(T) r ) { |
---|
| 137 | return (rational(T)){ -r.numerator, r.denominator }; |
---|
[3ce0d440] | 138 | } // -? |
---|
| 139 | |
---|
[541dbc09] | 140 | rational(T) ?+?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 141 | if ( l.denominator == r.denominator ) { // special case |
---|
[541dbc09] | 142 | return (rational(T)){ l.numerator + r.numerator, l.denominator }; |
---|
[3ce0d440] | 143 | } else { |
---|
[541dbc09] | 144 | return (rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; |
---|
[3ce0d440] | 145 | } // if |
---|
| 146 | } // ?+? |
---|
| 147 | |
---|
[541dbc09] | 148 | rational(T) ?+=?( rational(T) & l, rational(T) r ) { |
---|
[5dc4c7e] | 149 | l = l + r; |
---|
| 150 | return l; |
---|
| 151 | } // ?+? |
---|
| 152 | |
---|
[541dbc09] | 153 | rational(T) ?+=?( rational(T) & l, one_t ) { |
---|
| 154 | l = l + (rational(T)){ 1 }; |
---|
[5dc4c7e] | 155 | return l; |
---|
| 156 | } // ?+? |
---|
| 157 | |
---|
[541dbc09] | 158 | rational(T) ?-?( rational(T) l, rational(T) r ) { |
---|
[3ce0d440] | 159 | if ( l.denominator == r.denominator ) { // special case |
---|
[541dbc09] | 160 | return (rational(T)){ l.numerator - r.numerator, l.denominator }; |
---|
[3ce0d440] | 161 | } else { |
---|
[541dbc09] | 162 | return (rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; |
---|
[3ce0d440] | 163 | } // if |
---|
| 164 | } // ?-? |
---|
| 165 | |
---|
[541dbc09] | 166 | rational(T) ?-=?( rational(T) & l, rational(T) r ) { |
---|
[5dc4c7e] | 167 | l = l - r; |
---|
| 168 | return l; |
---|
| 169 | } // ?-? |
---|
| 170 | |
---|
[541dbc09] | 171 | rational(T) ?-=?( rational(T) & l, one_t ) { |
---|
| 172 | l = l - (rational(T)){ 1 }; |
---|
[5dc4c7e] | 173 | return l; |
---|
| 174 | } // ?-? |
---|
| 175 | |
---|
[541dbc09] | 176 | rational(T) ?*?( rational(T) l, rational(T) r ) { |
---|
| 177 | return (rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; |
---|
[5dc4c7e] | 178 | } // ?*? |
---|
| 179 | |
---|
[541dbc09] | 180 | rational(T) ?*=?( rational(T) & l, rational(T) r ) { |
---|
[5dc4c7e] | 181 | return l = l * r; |
---|
[3ce0d440] | 182 | } // ?*? |
---|
| 183 | |
---|
[541dbc09] | 184 | rational(T) ?/?( rational(T) l, rational(T) r ) { |
---|
[5dc4c7e] | 185 | if ( r.numerator < (T){0} ) { |
---|
[0087e0e] | 186 | r.[numerator, denominator] = [-r.numerator, -r.denominator]; |
---|
[3ce0d440] | 187 | } // if |
---|
[541dbc09] | 188 | return (rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator }; |
---|
[5dc4c7e] | 189 | } // ?/? |
---|
| 190 | |
---|
[541dbc09] | 191 | rational(T) ?/=?( rational(T) & l, rational(T) r ) { |
---|
[5dc4c7e] | 192 | return l = l / r; |
---|
[3ce0d440] | 193 | } // ?/? |
---|
[f5e37a4] | 194 | } // distribution |
---|
[3ce0d440] | 195 | |
---|
[f5e37a4] | 196 | // I/O |
---|
[3ce0d440] | 197 | |
---|
[f5e37a4] | 198 | forall( T ) { |
---|
| 199 | forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } | arithmetic( T ) ) |
---|
[541dbc09] | 200 | istype & ?|?( istype & is, rational(T) & r ) { |
---|
[3ce0d440] | 201 | is | r.numerator | r.denominator; |
---|
[5dc4c7e] | 202 | T t = simplify( r.numerator, r.denominator ); |
---|
[3ce0d440] | 203 | r.numerator /= t; |
---|
| 204 | r.denominator /= t; |
---|
| 205 | return is; |
---|
| 206 | } // ?|? |
---|
| 207 | |
---|
[5dc4c7e] | 208 | forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { |
---|
[74cbaa3] | 209 | ostype & ?|?( ostype & os, rational(T) r ) { |
---|
[200fcb3] | 210 | return os | r.numerator | '/' | r.denominator; |
---|
| 211 | } // ?|? |
---|
[b12e4ad] | 212 | OSTYPE_VOID_IMPL( os, rational(T) ) |
---|
[200fcb3] | 213 | } // distribution |
---|
[3ce0d440] | 214 | } // distribution |
---|
[630a82a] | 215 | |
---|
[f5e37a4] | 216 | // Exponentiation |
---|
| 217 | |
---|
[541dbc09] | 218 | forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { |
---|
[47174c4] | 219 | rational(T) ?\?( rational(T) x, long int y ) { |
---|
[5dc4c7e] | 220 | if ( y < 0 ) { |
---|
[541dbc09] | 221 | return (rational(T)){ x.denominator \ -y, x.numerator \ -y }; |
---|
[5dc4c7e] | 222 | } else { |
---|
[541dbc09] | 223 | return (rational(T)){ x.numerator \ y, x.denominator \ y }; |
---|
[5dc4c7e] | 224 | } // if |
---|
| 225 | } // ?\? |
---|
| 226 | |
---|
[541dbc09] | 227 | rational(T) ?\=?( rational(T) & x, long int y ) { |
---|
[5dc4c7e] | 228 | return x = x \ y; |
---|
| 229 | } // ?\? |
---|
| 230 | } // distribution |
---|
[0087e0e] | 231 | |
---|
[f5e37a4] | 232 | // Conversion |
---|
[630a82a] | 233 | |
---|
[541dbc09] | 234 | forall( T | arithmetic( T ) | { double convert( T ); } ) |
---|
| 235 | double widen( rational(T) r ) { |
---|
[6c6455f] | 236 | return convert( r.numerator ) / convert( r.denominator ); |
---|
| 237 | } // widen |
---|
| 238 | |
---|
[541dbc09] | 239 | forall( T | arithmetic( T ) | { double convert( T ); T convert( double ); } ) |
---|
| 240 | rational(T) narrow( double f, T md ) { |
---|
[3ce0d440] | 241 | // http://www.ics.uci.edu/~eppstein/numth/frap.c |
---|
[541dbc09] | 242 | if ( md <= (T){1} ) { // maximum fractional digits too small? |
---|
| 243 | return (rational(T)){ convert( f ), (T){1}}; // truncate fraction |
---|
[6c6455f] | 244 | } // if |
---|
| 245 | |
---|
| 246 | // continued fraction coefficients |
---|
[5dc4c7e] | 247 | T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; |
---|
| 248 | T ai, t; |
---|
[6c6455f] | 249 | |
---|
| 250 | // find terms until denom gets too big |
---|
[f6a4917] | 251 | for () { |
---|
[6c6455f] | 252 | ai = convert( f ); |
---|
| 253 | if ( ! (m10 * ai + m11 <= md) ) break; |
---|
| 254 | t = m00 * ai + m01; |
---|
| 255 | m01 = m00; |
---|
| 256 | m00 = t; |
---|
| 257 | t = m10 * ai + m11; |
---|
| 258 | m11 = m10; |
---|
| 259 | m10 = t; |
---|
| 260 | double temp = convert( ai ); |
---|
| 261 | if ( f == temp ) break; // prevent division by zero |
---|
| 262 | f = 1 / (f - temp); |
---|
| 263 | if ( f > (double)0x7FFFFFFF ) break; // representation failure |
---|
| 264 | } // for |
---|
[541dbc09] | 265 | return (rational(T)){ m00, m10 }; |
---|
[6c6455f] | 266 | } // narrow |
---|
[53ba273] | 267 | |
---|
| 268 | // Local Variables: // |
---|
| 269 | // tab-width: 4 // |
---|
| 270 | // End: // |
---|