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