[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 |
---|
[f6a4917] | 12 | // Last Modified On : Thu Aug 25 18:09:58 2022 |
---|
| 13 | // Update Count : 194 |
---|
[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 | |
---|
[5dc4c7e] | 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 |
---|
[5dc4c7e] | 41 | if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator |
---|
[3ce0d440] | 42 | return gcd( abs( n ), d ); // simplify |
---|
| 43 | } // Rationalnumber::simplify |
---|
| 44 | |
---|
| 45 | // constructors |
---|
| 46 | |
---|
[5dc4c7e] | 47 | void ?{}( Rational(T) & r, zero_t ) { |
---|
| 48 | r{ (T){0}, (T){1} }; |
---|
[3ce0d440] | 49 | } // rational |
---|
| 50 | |
---|
[5dc4c7e] | 51 | void ?{}( Rational(T) & r, one_t ) { |
---|
| 52 | r{ (T){1}, (T){1} }; |
---|
[3ce0d440] | 53 | } // rational |
---|
| 54 | |
---|
[5dc4c7e] | 55 | void ?{}( Rational(T) & r ) { |
---|
| 56 | r{ (T){0}, (T){1} }; |
---|
[3ce0d440] | 57 | } // rational |
---|
| 58 | |
---|
[5dc4c7e] | 59 | void ?{}( Rational(T) & r, T n ) { |
---|
| 60 | r{ n, (T){1} }; |
---|
[f00b2c2c] | 61 | } // rational |
---|
| 62 | |
---|
[5dc4c7e] | 63 | void ?{}( Rational(T) & r, T n, T d ) { |
---|
| 64 | T t = simplify( n, d ); // simplify |
---|
| 65 | r.[numerator, denominator] = [n / t, d / t]; |
---|
[f00b2c2c] | 66 | } // rational |
---|
[3ce0d440] | 67 | |
---|
| 68 | // getter for numerator/denominator |
---|
| 69 | |
---|
[5dc4c7e] | 70 | T numerator( Rational(T) r ) { |
---|
[3ce0d440] | 71 | return r.numerator; |
---|
| 72 | } // numerator |
---|
| 73 | |
---|
[5dc4c7e] | 74 | T denominator( Rational(T) r ) { |
---|
[3ce0d440] | 75 | return r.denominator; |
---|
| 76 | } // denominator |
---|
| 77 | |
---|
[5dc4c7e] | 78 | [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ) { |
---|
[3ce0d440] | 79 | return dest = src.[ numerator, denominator ]; |
---|
| 80 | } // ?=? |
---|
| 81 | |
---|
| 82 | // setter for numerator/denominator |
---|
| 83 | |
---|
[5dc4c7e] | 84 | T numerator( Rational(T) r, T n ) { |
---|
| 85 | T prev = r.numerator; |
---|
| 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 | |
---|
[5dc4c7e] | 91 | T denominator( Rational(T) r, T d ) { |
---|
| 92 | T prev = r.denominator; |
---|
| 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 | |
---|
[5dc4c7e] | 100 | int ?==?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 101 | return l.numerator * r.denominator == l.denominator * r.numerator; |
---|
| 102 | } // ?==? |
---|
| 103 | |
---|
[5dc4c7e] | 104 | int ?!=?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 105 | return ! ( l == r ); |
---|
| 106 | } // ?!=? |
---|
| 107 | |
---|
[5dc4c7e] | 108 | int ?!=?( Rational(T) l, zero_t ) { |
---|
| 109 | return ! ( l == (Rational(T)){ 0 } ); |
---|
| 110 | } // ?!=? |
---|
| 111 | |
---|
| 112 | int ?<?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 113 | return l.numerator * r.denominator < l.denominator * r.numerator; |
---|
| 114 | } // ?<? |
---|
| 115 | |
---|
[5dc4c7e] | 116 | int ?<=?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 117 | return l.numerator * r.denominator <= l.denominator * r.numerator; |
---|
| 118 | } // ?<=? |
---|
| 119 | |
---|
[5dc4c7e] | 120 | int ?>?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 121 | return ! ( l <= r ); |
---|
| 122 | } // ?>? |
---|
| 123 | |
---|
[5dc4c7e] | 124 | int ?>=?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 125 | return ! ( l < r ); |
---|
| 126 | } // ?>=? |
---|
| 127 | |
---|
| 128 | // arithmetic |
---|
| 129 | |
---|
[5dc4c7e] | 130 | Rational(T) +?( Rational(T) r ) { |
---|
| 131 | return (Rational(T)){ r.numerator, r.denominator }; |
---|
[3ce0d440] | 132 | } // +? |
---|
[53ba273] | 133 | |
---|
[5dc4c7e] | 134 | Rational(T) -?( Rational(T) r ) { |
---|
| 135 | return (Rational(T)){ -r.numerator, r.denominator }; |
---|
[3ce0d440] | 136 | } // -? |
---|
| 137 | |
---|
[5dc4c7e] | 138 | Rational(T) ?+?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 139 | if ( l.denominator == r.denominator ) { // special case |
---|
[5dc4c7e] | 140 | return (Rational(T)){ l.numerator + r.numerator, l.denominator }; |
---|
[3ce0d440] | 141 | } else { |
---|
[5dc4c7e] | 142 | return (Rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; |
---|
[3ce0d440] | 143 | } // if |
---|
| 144 | } // ?+? |
---|
| 145 | |
---|
[5dc4c7e] | 146 | Rational(T) ?+=?( Rational(T) & l, Rational(T) r ) { |
---|
| 147 | l = l + r; |
---|
| 148 | return l; |
---|
| 149 | } // ?+? |
---|
| 150 | |
---|
| 151 | Rational(T) ?+=?( Rational(T) & l, one_t ) { |
---|
| 152 | l = l + (Rational(T)){ 1 }; |
---|
| 153 | return l; |
---|
| 154 | } // ?+? |
---|
| 155 | |
---|
| 156 | Rational(T) ?-?( Rational(T) l, Rational(T) r ) { |
---|
[3ce0d440] | 157 | if ( l.denominator == r.denominator ) { // special case |
---|
[5dc4c7e] | 158 | return (Rational(T)){ l.numerator - r.numerator, l.denominator }; |
---|
[3ce0d440] | 159 | } else { |
---|
[5dc4c7e] | 160 | return (Rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; |
---|
[3ce0d440] | 161 | } // if |
---|
| 162 | } // ?-? |
---|
| 163 | |
---|
[5dc4c7e] | 164 | Rational(T) ?-=?( Rational(T) & l, Rational(T) r ) { |
---|
| 165 | l = l - r; |
---|
| 166 | return l; |
---|
| 167 | } // ?-? |
---|
| 168 | |
---|
| 169 | Rational(T) ?-=?( Rational(T) & l, one_t ) { |
---|
| 170 | l = l - (Rational(T)){ 1 }; |
---|
| 171 | return l; |
---|
| 172 | } // ?-? |
---|
| 173 | |
---|
| 174 | Rational(T) ?*?( Rational(T) l, Rational(T) r ) { |
---|
| 175 | return (Rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; |
---|
| 176 | } // ?*? |
---|
| 177 | |
---|
| 178 | Rational(T) ?*=?( Rational(T) & l, Rational(T) r ) { |
---|
| 179 | return l = l * r; |
---|
[3ce0d440] | 180 | } // ?*? |
---|
| 181 | |
---|
[5dc4c7e] | 182 | Rational(T) ?/?( Rational(T) l, Rational(T) r ) { |
---|
| 183 | if ( r.numerator < (T){0} ) { |
---|
[0087e0e] | 184 | r.[numerator, denominator] = [-r.numerator, -r.denominator]; |
---|
[3ce0d440] | 185 | } // if |
---|
[5dc4c7e] | 186 | return (Rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator }; |
---|
| 187 | } // ?/? |
---|
| 188 | |
---|
| 189 | Rational(T) ?/=?( Rational(T) & l, Rational(T) r ) { |
---|
| 190 | return l = l / r; |
---|
[3ce0d440] | 191 | } // ?/? |
---|
| 192 | |
---|
| 193 | // I/O |
---|
| 194 | |
---|
[5dc4c7e] | 195 | forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) |
---|
| 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 ); } ) { |
---|
| 205 | ostype & ?|?( ostype & os, Rational(T) r ) { |
---|
[200fcb3] | 206 | return os | r.numerator | '/' | r.denominator; |
---|
| 207 | } // ?|? |
---|
| 208 | |
---|
[5dc4c7e] | 209 | void ?|?( ostype & os, Rational(T) r ) { |
---|
[65240bb] | 210 | (ostype &)(os | r); ends( os ); |
---|
[200fcb3] | 211 | } // ?|? |
---|
| 212 | } // distribution |
---|
[3ce0d440] | 213 | } // distribution |
---|
[630a82a] | 214 | |
---|
[5dc4c7e] | 215 | forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { |
---|
| 216 | Rational(T) ?\?( Rational(T) x, long int y ) { |
---|
| 217 | if ( y < 0 ) { |
---|
| 218 | return (Rational(T)){ x.denominator \ -y, x.numerator \ -y }; |
---|
| 219 | } else { |
---|
| 220 | return (Rational(T)){ x.numerator \ y, x.denominator \ y }; |
---|
| 221 | } // if |
---|
| 222 | } // ?\? |
---|
| 223 | |
---|
| 224 | Rational(T) ?\=?( Rational(T) & x, long int y ) { |
---|
| 225 | return x = x \ y; |
---|
| 226 | } // ?\? |
---|
| 227 | } // distribution |
---|
[0087e0e] | 228 | |
---|
[630a82a] | 229 | // conversion |
---|
| 230 | |
---|
[5dc4c7e] | 231 | forall( T | Arithmetic( T ) | { double convert( T ); } ) |
---|
| 232 | double widen( Rational(T) r ) { |
---|
[6c6455f] | 233 | return convert( r.numerator ) / convert( r.denominator ); |
---|
| 234 | } // widen |
---|
| 235 | |
---|
[5dc4c7e] | 236 | forall( T | Arithmetic( T ) | { double convert( T ); T convert( double ); } ) |
---|
| 237 | Rational(T) narrow( double f, T md ) { |
---|
[3ce0d440] | 238 | // http://www.ics.uci.edu/~eppstein/numth/frap.c |
---|
[5dc4c7e] | 239 | if ( md <= (T){1} ) { // maximum fractional digits too small? |
---|
| 240 | return (Rational(T)){ convert( f ), (T){1}}; // truncate fraction |
---|
[6c6455f] | 241 | } // if |
---|
| 242 | |
---|
| 243 | // continued fraction coefficients |
---|
[5dc4c7e] | 244 | T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; |
---|
| 245 | T ai, t; |
---|
[6c6455f] | 246 | |
---|
| 247 | // find terms until denom gets too big |
---|
[f6a4917] | 248 | for () { |
---|
[6c6455f] | 249 | ai = convert( f ); |
---|
| 250 | if ( ! (m10 * ai + m11 <= md) ) break; |
---|
| 251 | t = m00 * ai + m01; |
---|
| 252 | m01 = m00; |
---|
| 253 | m00 = t; |
---|
| 254 | t = m10 * ai + m11; |
---|
| 255 | m11 = m10; |
---|
| 256 | m10 = t; |
---|
| 257 | double temp = convert( ai ); |
---|
| 258 | if ( f == temp ) break; // prevent division by zero |
---|
| 259 | f = 1 / (f - temp); |
---|
| 260 | if ( f > (double)0x7FFFFFFF ) break; // representation failure |
---|
| 261 | } // for |
---|
[5dc4c7e] | 262 | return (Rational(T)){ m00, m10 }; |
---|
[6c6455f] | 263 | } // narrow |
---|
[53ba273] | 264 | |
---|
| 265 | // Local Variables: // |
---|
| 266 | // tab-width: 4 // |
---|
| 267 | // End: // |
---|