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