| [53ba273] | 1 | // 
 | 
|---|
 | 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.
 | 
|---|
 | 6 | // 
 | 
|---|
 | 7 | // rational.c -- 
 | 
|---|
 | 8 | // 
 | 
|---|
 | 9 | // Author           : Peter A. Buhr
 | 
|---|
 | 10 | // Created On       : Wed Apr  6 17:54:28 2016
 | 
|---|
 | 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| [6c6455f] | 12 | // Last Modified On : Mon May 15 21:29:23 2017
 | 
|---|
 | 13 | // Update Count     : 149
 | 
|---|
| [53ba273] | 14 | // 
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | #include "rational"
 | 
|---|
| [3d9b5da] | 17 | #include "fstream"
 | 
|---|
 | 18 | #include "stdlib"
 | 
|---|
| [53ba273] | 19 | 
 | 
|---|
| [45161b4d] | 20 | // helper routines
 | 
|---|
| [630a82a] | 21 | 
 | 
|---|
 | 22 | // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
 | 
|---|
| [45161b4d] | 23 | // alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
 | 
|---|
| [561f730] | 24 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
| [f621a148] | 25 | static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
 | 
|---|
| [6e4b913] | 26 |         for ( ;; ) {                                                                            // Euclid's algorithm
 | 
|---|
| [f621a148] | 27 |                 RationalImpl r = a % b;
 | 
|---|
| [561f730] | 28 |           if ( r == (RationalImpl){0} ) break;
 | 
|---|
| [53ba273] | 29 |                 a = b;
 | 
|---|
 | 30 |                 b = r;
 | 
|---|
| [6e4b913] | 31 |         } // for
 | 
|---|
| [53ba273] | 32 |         return b;
 | 
|---|
 | 33 | } // gcd
 | 
|---|
 | 34 | 
 | 
|---|
| [561f730] | 35 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 36 | static RationalImpl simplify( RationalImpl * n, RationalImpl * d ) {
 | 
|---|
 | 37 |         if ( *d == (RationalImpl){0} ) {
 | 
|---|
| [53ba273] | 38 |                 serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
 | 
|---|
 | 39 |                 exit( EXIT_FAILURE );
 | 
|---|
| [6e4b913] | 40 |         } // exit
 | 
|---|
| [561f730] | 41 |         if ( *d < (RationalImpl){0} ) { *d = -*d; *n = -*n; } // move sign to numerator
 | 
|---|
| [6e4b913] | 42 |         return gcd( abs( *n ), *d );                                            // simplify
 | 
|---|
| [53ba273] | 43 | } // Rationalnumber::simplify
 | 
|---|
 | 44 | 
 | 
|---|
| [630a82a] | 45 | 
 | 
|---|
 | 46 | // constructors
 | 
|---|
 | 47 | 
 | 
|---|
| [561f730] | 48 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 49 | void ?{}( Rational(RationalImpl) * r ) {
 | 
|---|
 | 50 |         r{ (RationalImpl){0}, (RationalImpl){1} };
 | 
|---|
| [53ba273] | 51 | } // rational
 | 
|---|
 | 52 | 
 | 
|---|
| [561f730] | 53 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 54 | void ?{}( Rational(RationalImpl) * r, RationalImpl n ) {
 | 
|---|
 | 55 |         r{ n, (RationalImpl){1} };
 | 
|---|
| [53ba273] | 56 | } // rational
 | 
|---|
 | 57 | 
 | 
|---|
| [561f730] | 58 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 59 | void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d ) {
 | 
|---|
| [f621a148] | 60 |         RationalImpl t = simplify( &n, &d );                            // simplify
 | 
|---|
| [6e4b913] | 61 |         r->numerator = n / t;
 | 
|---|
| [d1ab5331] | 62 |         r->denominator = d / t;
 | 
|---|
| [53ba273] | 63 | } // rational
 | 
|---|
 | 64 | 
 | 
|---|
| [630a82a] | 65 | 
 | 
|---|
| [f621a148] | 66 | // getter for numerator/denominator
 | 
|---|
| [630a82a] | 67 | 
 | 
|---|
| [561f730] | 68 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 69 | RationalImpl numerator( Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 70 |         return r.numerator;
 | 
|---|
| [53ba273] | 71 | } // numerator
 | 
|---|
 | 72 | 
 | 
|---|
| [561f730] | 73 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 74 | RationalImpl denominator( Rational(RationalImpl) r ) {
 | 
|---|
| [f621a148] | 75 |         return r.denominator;
 | 
|---|
 | 76 | } // denominator
 | 
|---|
 | 77 | 
 | 
|---|
| [561f730] | 78 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 79 | [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
 | 
|---|
| [f621a148] | 80 |         return *dest = src.[ numerator, denominator ];
 | 
|---|
 | 81 | }
 | 
|---|
 | 82 | 
 | 
|---|
 | 83 | // setter for numerator/denominator
 | 
|---|
 | 84 | 
 | 
|---|
| [561f730] | 85 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 86 | RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
 | 
|---|
| [f621a148] | 87 |         RationalImpl prev = r.numerator;
 | 
|---|
 | 88 |         RationalImpl t = gcd( abs( n ), r.denominator );                // simplify
 | 
|---|
| [6e4b913] | 89 |         r.numerator = n / t;
 | 
|---|
 | 90 |         r.denominator = r.denominator / t;
 | 
|---|
 | 91 |         return prev;
 | 
|---|
| [53ba273] | 92 | } // numerator
 | 
|---|
 | 93 | 
 | 
|---|
| [561f730] | 94 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 95 | RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
 | 
|---|
| [f621a148] | 96 |         RationalImpl prev = r.denominator;
 | 
|---|
 | 97 |         RationalImpl t = simplify( &r.numerator, &d );                  // simplify
 | 
|---|
| [6e4b913] | 98 |         r.numerator = r.numerator / t;
 | 
|---|
 | 99 |         r.denominator = d / t;
 | 
|---|
 | 100 |         return prev;
 | 
|---|
| [53ba273] | 101 | } // denominator
 | 
|---|
 | 102 | 
 | 
|---|
| [630a82a] | 103 | 
 | 
|---|
 | 104 | // comparison
 | 
|---|
 | 105 | 
 | 
|---|
| [561f730] | 106 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 107 | int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 108 |         return l.numerator * r.denominator == l.denominator * r.numerator;
 | 
|---|
| [53ba273] | 109 | } // ?==?
 | 
|---|
 | 110 | 
 | 
|---|
| [561f730] | 111 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 112 | int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 113 |         return ! ( l == r );
 | 
|---|
| [53ba273] | 114 | } // ?!=?
 | 
|---|
 | 115 | 
 | 
|---|
| [561f730] | 116 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 117 | int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 118 |         return l.numerator * r.denominator < l.denominator * r.numerator;
 | 
|---|
| [53ba273] | 119 | } // ?<?
 | 
|---|
 | 120 | 
 | 
|---|
| [561f730] | 121 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 122 | int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
 | 123 |         return l.numerator * r.denominator <= l.denominator * r.numerator;
 | 
|---|
| [53ba273] | 124 | } // ?<=?
 | 
|---|
 | 125 | 
 | 
|---|
| [561f730] | 126 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 127 | int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 128 |         return ! ( l <= r );
 | 
|---|
| [53ba273] | 129 | } // ?>?
 | 
|---|
 | 130 | 
 | 
|---|
| [561f730] | 131 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 132 | int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 133 |         return ! ( l < r );
 | 
|---|
| [53ba273] | 134 | } // ?>=?
 | 
|---|
 | 135 | 
 | 
|---|
| [630a82a] | 136 | 
 | 
|---|
 | 137 | // arithmetic
 | 
|---|
 | 138 | 
 | 
|---|
| [561f730] | 139 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 140 | Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
 | 
|---|
 | 141 |         Rational(RationalImpl) t = { r.numerator, r.denominator };
 | 
|---|
 | 142 |         return t;
 | 
|---|
 | 143 | } // +?
 | 
|---|
 | 144 | 
 | 
|---|
 | 145 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 146 | Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
 | 
|---|
 | 147 |         Rational(RationalImpl) t = { -r.numerator, r.denominator };
 | 
|---|
| [6e4b913] | 148 |         return t;
 | 
|---|
| [53ba273] | 149 | } // -?
 | 
|---|
 | 150 | 
 | 
|---|
| [561f730] | 151 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 152 | Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 153 |         if ( l.denominator == r.denominator ) {                         // special case
 | 
|---|
| [561f730] | 154 |                 Rational(RationalImpl) t = { l.numerator + r.numerator, l.denominator };
 | 
|---|
| [53ba273] | 155 |                 return t;
 | 
|---|
| [6e4b913] | 156 |         } else {
 | 
|---|
| [561f730] | 157 |                 Rational(RationalImpl) t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
 | 
|---|
| [53ba273] | 158 |                 return t;
 | 
|---|
| [6e4b913] | 159 |         } // if
 | 
|---|
| [53ba273] | 160 | } // ?+?
 | 
|---|
 | 161 | 
 | 
|---|
| [561f730] | 162 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 163 | Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
| [6e4b913] | 164 |         if ( l.denominator == r.denominator ) {                         // special case
 | 
|---|
| [561f730] | 165 |                 Rational(RationalImpl) t = { l.numerator - r.numerator, l.denominator };
 | 
|---|
| [53ba273] | 166 |                 return t;
 | 
|---|
| [6e4b913] | 167 |         } else {
 | 
|---|
| [561f730] | 168 |                 Rational(RationalImpl) t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
 | 
|---|
| [53ba273] | 169 |                 return t;
 | 
|---|
| [6e4b913] | 170 |         } // if
 | 
|---|
| [53ba273] | 171 | } // ?-?
 | 
|---|
 | 172 | 
 | 
|---|
| [561f730] | 173 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 174 | Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
 | 175 |         Rational(RationalImpl) t = { l.numerator * r.numerator, l.denominator * r.denominator };
 | 
|---|
| [53ba273] | 176 |         return t;
 | 
|---|
 | 177 | } // ?*?
 | 
|---|
 | 178 | 
 | 
|---|
| [561f730] | 179 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 180 | Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
 | 
|---|
 | 181 |         if ( r.numerator < (RationalImpl){0} ) {
 | 
|---|
| [53ba273] | 182 |                 r.numerator = -r.numerator;
 | 
|---|
 | 183 |                 r.denominator = -r.denominator;
 | 
|---|
 | 184 |         } // if
 | 
|---|
| [561f730] | 185 |         Rational(RationalImpl) t = { l.numerator * r.denominator, l.denominator * r.numerator };
 | 
|---|
| [6e4b913] | 186 |         return t;
 | 
|---|
| [53ba273] | 187 | } // ?/?
 | 
|---|
 | 188 | 
 | 
|---|
| [630a82a] | 189 | 
 | 
|---|
 | 190 | // conversion
 | 
|---|
 | 191 | 
 | 
|---|
| [6c6455f] | 192 | forall ( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
 | 
|---|
 | 193 | double widen( Rational(RationalImpl) r ) {
 | 
|---|
 | 194 |         return convert( r.numerator ) / convert( r.denominator );
 | 
|---|
 | 195 | } // widen
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 | // http://www.ics.uci.edu/~eppstein/numth/frap.c
 | 
|---|
 | 198 | forall ( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
 | 
|---|
 | 199 | Rational(RationalImpl) narrow( double f, RationalImpl md ) {
 | 
|---|
 | 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 | 
 | 
|---|
| [630a82a] | 226 | 
 | 
|---|
 | 227 | // I/O
 | 
|---|
 | 228 | 
 | 
|---|
| [561f730] | 229 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 230 | forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } )
 | 
|---|
 | 231 | istype * ?|?( istype * is, Rational(RationalImpl) * r ) {
 | 
|---|
| [f621a148] | 232 |         RationalImpl t;
 | 
|---|
| [6e4b913] | 233 |         is | &(r->numerator) | &(r->denominator);
 | 
|---|
| [53ba273] | 234 |         t = simplify( &(r->numerator), &(r->denominator) );
 | 
|---|
| [6e4b913] | 235 |         r->numerator /= t;
 | 
|---|
 | 236 |         r->denominator /= t;
 | 
|---|
 | 237 |         return is;
 | 
|---|
| [53ba273] | 238 | } // ?|?
 | 
|---|
 | 239 | 
 | 
|---|
| [561f730] | 240 | forall ( otype RationalImpl | arithmetic( RationalImpl ) )
 | 
|---|
 | 241 | forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } )
 | 
|---|
 | 242 | ostype * ?|?( ostype * os, Rational(RationalImpl ) r ) {
 | 
|---|
| [6e4b913] | 243 |         return os | r.numerator | '/' | r.denominator;
 | 
|---|
| [53ba273] | 244 | } // ?|?
 | 
|---|
 | 245 | 
 | 
|---|
 | 246 | // Local Variables: //
 | 
|---|
 | 247 | // tab-width: 4 //
 | 
|---|
 | 248 | // End: //
 | 
|---|