[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 |
---|
[74cbaa3] | 12 | // Last Modified On : Fri Nov 8 17:00:38 2024 |
---|
| 13 | // Update Count : 205 |
---|
[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 | } // ?|? |
---|
| 203 | |
---|
[5dc4c7e] | 204 | forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { |
---|
[74cbaa3] | 205 | ostype & ?|?( ostype & os, rational(T) r ) { |
---|
[200fcb3] | 206 | return os | r.numerator | '/' | r.denominator; |
---|
| 207 | } // ?|? |
---|
[b12e4ad] | 208 | OSTYPE_VOID_IMPL( os, rational(T) ) |
---|
[200fcb3] | 209 | } // distribution |
---|
[3ce0d440] | 210 | } // distribution |
---|
[630a82a] | 211 | |
---|
[541dbc09] | 212 | forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { |
---|
[74cbaa3] | 213 | rational(T) ?\?( rational(T) x, long int y ) { |
---|
[5dc4c7e] | 214 | if ( y < 0 ) { |
---|
[541dbc09] | 215 | return (rational(T)){ x.denominator \ -y, x.numerator \ -y }; |
---|
[5dc4c7e] | 216 | } else { |
---|
[541dbc09] | 217 | return (rational(T)){ x.numerator \ y, x.denominator \ y }; |
---|
[5dc4c7e] | 218 | } // if |
---|
| 219 | } // ?\? |
---|
| 220 | |
---|
[541dbc09] | 221 | rational(T) ?\=?( rational(T) & x, long int y ) { |
---|
[5dc4c7e] | 222 | return x = x \ y; |
---|
| 223 | } // ?\? |
---|
| 224 | } // distribution |
---|
[0087e0e] | 225 | |
---|
[630a82a] | 226 | // conversion |
---|
| 227 | |
---|
[541dbc09] | 228 | forall( T | arithmetic( T ) | { double convert( T ); } ) |
---|
| 229 | double widen( rational(T) r ) { |
---|
[6c6455f] | 230 | return convert( r.numerator ) / convert( r.denominator ); |
---|
| 231 | } // widen |
---|
| 232 | |
---|
[541dbc09] | 233 | forall( T | arithmetic( T ) | { double convert( T ); T convert( double ); } ) |
---|
| 234 | rational(T) narrow( double f, T md ) { |
---|
[3ce0d440] | 235 | // http://www.ics.uci.edu/~eppstein/numth/frap.c |
---|
[541dbc09] | 236 | if ( md <= (T){1} ) { // maximum fractional digits too small? |
---|
| 237 | return (rational(T)){ convert( f ), (T){1}}; // truncate fraction |
---|
[6c6455f] | 238 | } // if |
---|
| 239 | |
---|
| 240 | // continued fraction coefficients |
---|
[5dc4c7e] | 241 | T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; |
---|
| 242 | T ai, t; |
---|
[6c6455f] | 243 | |
---|
| 244 | // find terms until denom gets too big |
---|
[f6a4917] | 245 | for () { |
---|
[6c6455f] | 246 | ai = convert( f ); |
---|
| 247 | if ( ! (m10 * ai + m11 <= md) ) break; |
---|
| 248 | t = m00 * ai + m01; |
---|
| 249 | m01 = m00; |
---|
| 250 | m00 = t; |
---|
| 251 | t = m10 * ai + m11; |
---|
| 252 | m11 = m10; |
---|
| 253 | m10 = t; |
---|
| 254 | double temp = convert( ai ); |
---|
| 255 | if ( f == temp ) break; // prevent division by zero |
---|
| 256 | f = 1 / (f - temp); |
---|
| 257 | if ( f > (double)0x7FFFFFFF ) break; // representation failure |
---|
| 258 | } // for |
---|
[541dbc09] | 259 | return (rational(T)){ m00, m10 }; |
---|
[6c6455f] | 260 | } // narrow |
---|
[53ba273] | 261 | |
---|
| 262 | // Local Variables: // |
---|
| 263 | // tab-width: 4 // |
---|
| 264 | // End: // |
---|