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