| [53ba273] | 1 | //                               -*- Mode: C -*- | 
|---|
|  | 2 | // | 
|---|
|  | 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
|  | 4 | // | 
|---|
|  | 5 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 6 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 7 | // | 
|---|
|  | 8 | // rational.c -- | 
|---|
|  | 9 | // | 
|---|
|  | 10 | // Author           : Peter A. Buhr | 
|---|
|  | 11 | // Created On       : Wed Apr  6 17:54:28 2016 | 
|---|
|  | 12 | // Last Modified By : Peter A. Buhr | 
|---|
| [d1ab5331] | 13 | // Last Modified On : Wed May  4 14:16:14 2016 | 
|---|
|  | 14 | // Update Count     : 25 | 
|---|
| [53ba273] | 15 | // | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include "rational" | 
|---|
| [3d9b5da] | 18 | #include "fstream" | 
|---|
|  | 19 | #include "stdlib" | 
|---|
| [6e991d6] | 20 | #include "math"                                                                                 // floor | 
|---|
| [53ba273] | 21 |  | 
|---|
| [630a82a] | 22 |  | 
|---|
|  | 23 | // constants | 
|---|
|  | 24 |  | 
|---|
| [53ba273] | 25 | struct Rational 0 = {0, 1}; | 
|---|
|  | 26 | struct Rational 1 = {1, 1}; | 
|---|
|  | 27 |  | 
|---|
|  | 28 |  | 
|---|
| [45161b4d] | 29 | // helper routines | 
|---|
| [630a82a] | 30 |  | 
|---|
|  | 31 | // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals. | 
|---|
| [45161b4d] | 32 | // alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm | 
|---|
| [630a82a] | 33 | static long int gcd( long int a, long int b ) { | 
|---|
| [53ba273] | 34 | for ( ;; ) {                                                                                // Euclid's algorithm | 
|---|
|  | 35 | long int r = a % b; | 
|---|
|  | 36 | if ( r == 0 ) break; | 
|---|
|  | 37 | a = b; | 
|---|
|  | 38 | b = r; | 
|---|
|  | 39 | } // for | 
|---|
|  | 40 | return b; | 
|---|
|  | 41 | } // gcd | 
|---|
|  | 42 |  | 
|---|
| [630a82a] | 43 | static long int simplify( long int *n, long int *d ) { | 
|---|
| [53ba273] | 44 | if ( *d == 0 ) { | 
|---|
|  | 45 | serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl; | 
|---|
|  | 46 | exit( EXIT_FAILURE ); | 
|---|
|  | 47 | } // exit | 
|---|
|  | 48 | if ( *d < 0 ) { *d = -*d; *n = -*n; }                               // move sign to numerator | 
|---|
|  | 49 | return gcd( abs( *n ), *d );                                                // simplify | 
|---|
|  | 50 | } // Rationalnumber::simplify | 
|---|
|  | 51 |  | 
|---|
| [630a82a] | 52 |  | 
|---|
|  | 53 | // constructors | 
|---|
|  | 54 |  | 
|---|
| [d1ab5331] | 55 | void ?{}( Rational * r ) { | 
|---|
|  | 56 | r{ 0, 1 }; | 
|---|
| [53ba273] | 57 | } // rational | 
|---|
|  | 58 |  | 
|---|
| [d1ab5331] | 59 | void ?{}( Rational * r, long int n ) { | 
|---|
|  | 60 | r{ n, 1 }; | 
|---|
| [53ba273] | 61 | } // rational | 
|---|
|  | 62 |  | 
|---|
| [d1ab5331] | 63 | void ?{}( Rational * r, long int n, long int d ) { | 
|---|
| [53ba273] | 64 | long int t = simplify( &n, &d );                                    // simplify | 
|---|
| [d1ab5331] | 65 | r->numerator = n / t; | 
|---|
|  | 66 | r->denominator = d / t; | 
|---|
| [53ba273] | 67 | } // rational | 
|---|
|  | 68 |  | 
|---|
| [630a82a] | 69 |  | 
|---|
|  | 70 | // getter/setter for numerator/denominator | 
|---|
|  | 71 |  | 
|---|
| [53ba273] | 72 | long int numerator( Rational r ) { | 
|---|
|  | 73 | return r.numerator; | 
|---|
|  | 74 | } // numerator | 
|---|
|  | 75 |  | 
|---|
|  | 76 | long int numerator( Rational r, long int n ) { | 
|---|
|  | 77 | long int prev = r.numerator; | 
|---|
|  | 78 | long int t = gcd( abs( n ), r.denominator );                // simplify | 
|---|
|  | 79 | r.numerator = n / t; | 
|---|
|  | 80 | r.denominator = r.denominator / t; | 
|---|
|  | 81 | return prev; | 
|---|
|  | 82 | } // numerator | 
|---|
|  | 83 |  | 
|---|
| [630a82a] | 84 | long int denominator( Rational r ) { | 
|---|
|  | 85 | return r.denominator; | 
|---|
|  | 86 | } // denominator | 
|---|
|  | 87 |  | 
|---|
| [53ba273] | 88 | long int denominator( Rational r, long int d ) { | 
|---|
|  | 89 | long int prev = r.denominator; | 
|---|
|  | 90 | long int t = simplify( &r.numerator, &d );                  // simplify | 
|---|
|  | 91 | r.numerator = r.numerator / t; | 
|---|
|  | 92 | r.denominator = d / t; | 
|---|
|  | 93 | return prev; | 
|---|
|  | 94 | } // denominator | 
|---|
|  | 95 |  | 
|---|
| [630a82a] | 96 |  | 
|---|
|  | 97 | // comparison | 
|---|
|  | 98 |  | 
|---|
| [53ba273] | 99 | int ?==?( Rational l, Rational r ) { | 
|---|
|  | 100 | return l.numerator * r.denominator == l.denominator * r.numerator; | 
|---|
|  | 101 | } // ?==? | 
|---|
|  | 102 |  | 
|---|
|  | 103 | int ?!=?( Rational l, Rational r ) { | 
|---|
|  | 104 | return ! ( l == r ); | 
|---|
|  | 105 | } // ?!=? | 
|---|
|  | 106 |  | 
|---|
|  | 107 | int ?<?( Rational l, Rational r ) { | 
|---|
|  | 108 | return l.numerator * r.denominator < l.denominator * r.numerator; | 
|---|
|  | 109 | } // ?<? | 
|---|
|  | 110 |  | 
|---|
|  | 111 | int ?<=?( Rational l, Rational r ) { | 
|---|
|  | 112 | return l < r || l == r; | 
|---|
|  | 113 | } // ?<=? | 
|---|
|  | 114 |  | 
|---|
|  | 115 | int ?>?( Rational l, Rational r ) { | 
|---|
|  | 116 | return ! ( l <= r ); | 
|---|
|  | 117 | } // ?>? | 
|---|
|  | 118 |  | 
|---|
|  | 119 | int ?>=?( Rational l, Rational r ) { | 
|---|
|  | 120 | return ! ( l < r ); | 
|---|
|  | 121 | } // ?>=? | 
|---|
|  | 122 |  | 
|---|
| [630a82a] | 123 |  | 
|---|
|  | 124 | // arithmetic | 
|---|
|  | 125 |  | 
|---|
| [53ba273] | 126 | Rational -?( Rational r ) { | 
|---|
|  | 127 | Rational t = { -r.numerator, r.denominator }; | 
|---|
|  | 128 | return t; | 
|---|
|  | 129 | } // -? | 
|---|
|  | 130 |  | 
|---|
|  | 131 | Rational ?+?( Rational l, Rational r ) { | 
|---|
|  | 132 | if ( l.denominator == r.denominator ) {                             // special case | 
|---|
|  | 133 | Rational t = { l.numerator + r.numerator, l.denominator }; | 
|---|
|  | 134 | return t; | 
|---|
|  | 135 | } else { | 
|---|
|  | 136 | Rational t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; | 
|---|
|  | 137 | return t; | 
|---|
|  | 138 | } // if | 
|---|
|  | 139 | } // ?+? | 
|---|
|  | 140 |  | 
|---|
|  | 141 | Rational ?-?( Rational l, Rational r ) { | 
|---|
|  | 142 | if ( l.denominator == r.denominator ) {                             // special case | 
|---|
|  | 143 | Rational t = { l.numerator - r.numerator, l.denominator }; | 
|---|
|  | 144 | return t; | 
|---|
|  | 145 | } else { | 
|---|
|  | 146 | Rational t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; | 
|---|
|  | 147 | return t; | 
|---|
|  | 148 | } // if | 
|---|
|  | 149 | } // ?-? | 
|---|
|  | 150 |  | 
|---|
|  | 151 | Rational ?*?( Rational l, Rational r ) { | 
|---|
|  | 152 | Rational t = { l.numerator * r.numerator, l.denominator * r.denominator }; | 
|---|
|  | 153 | return t; | 
|---|
|  | 154 | } // ?*? | 
|---|
|  | 155 |  | 
|---|
|  | 156 | Rational ?/?( Rational l, Rational r ) { | 
|---|
|  | 157 | if ( r.numerator < 0 ) { | 
|---|
|  | 158 | r.numerator = -r.numerator; | 
|---|
|  | 159 | r.denominator = -r.denominator; | 
|---|
|  | 160 | } // if | 
|---|
|  | 161 | Rational t = { l.numerator * r.denominator, l.denominator * r.numerator }; | 
|---|
|  | 162 | return t; | 
|---|
|  | 163 | } // ?/? | 
|---|
|  | 164 |  | 
|---|
| [630a82a] | 165 |  | 
|---|
|  | 166 | // conversion | 
|---|
|  | 167 |  | 
|---|
| [53ba273] | 168 | double widen( Rational r ) { | 
|---|
|  | 169 | return (double)r.numerator / (double)r.denominator; | 
|---|
|  | 170 | } // widen | 
|---|
|  | 171 |  | 
|---|
|  | 172 | // https://rosettacode.org/wiki/Convert_decimal_number_to_rational#C | 
|---|
|  | 173 | Rational narrow( double f, long int md ) { | 
|---|
|  | 174 | if ( md <= 1 ) {                                                                        // maximum fractional digits too small? | 
|---|
| [d1ab5331] | 175 | return (Rational){ f, 1};                                               // truncate fraction | 
|---|
| [53ba273] | 176 | } // if | 
|---|
|  | 177 |  | 
|---|
|  | 178 | // continued fraction coefficients | 
|---|
|  | 179 | long int a, h[3] = { 0, 1, 0 }, k[3] = { 1, 0, 0 }; | 
|---|
|  | 180 | long int x, d, n = 1; | 
|---|
|  | 181 | int i, neg = 0; | 
|---|
|  | 182 |  | 
|---|
|  | 183 | if ( f < 0 ) { neg = 1; f = -f; } | 
|---|
|  | 184 | while ( f != floor( f ) ) { n <<= 1; f *= 2; } | 
|---|
|  | 185 | d = f; | 
|---|
|  | 186 |  | 
|---|
|  | 187 | // continued fraction and check denominator each step | 
|---|
|  | 188 | for (i = 0; i < 64; i++) { | 
|---|
|  | 189 | a = n ? d / n : 0; | 
|---|
|  | 190 | if (i && !a) break; | 
|---|
|  | 191 | x = d; d = n; n = x % n; | 
|---|
|  | 192 | x = a; | 
|---|
|  | 193 | if (k[1] * a + k[0] >= md) { | 
|---|
|  | 194 | x = (md - k[0]) / k[1]; | 
|---|
|  | 195 | if ( ! (x * 2 >= a || k[1] >= md) ) break; | 
|---|
|  | 196 | i = 65; | 
|---|
|  | 197 | } // if | 
|---|
|  | 198 | h[2] = x * h[1] + h[0]; h[0] = h[1]; h[1] = h[2]; | 
|---|
|  | 199 | k[2] = x * k[1] + k[0]; k[0] = k[1]; k[1] = k[2]; | 
|---|
|  | 200 | } // for | 
|---|
| [d1ab5331] | 201 | return (Rational){ neg ? -h[1] : h[1], k[1] }; | 
|---|
| [53ba273] | 202 | } // narrow | 
|---|
|  | 203 |  | 
|---|
| [630a82a] | 204 |  | 
|---|
|  | 205 | // I/O | 
|---|
|  | 206 |  | 
|---|
| [3d9b5da] | 207 | forall( dtype istype | istream( istype ) ) | 
|---|
|  | 208 | istype * ?|?( istype *is, Rational *r ) { | 
|---|
| [53ba273] | 209 | long int t; | 
|---|
|  | 210 | is | &(r->numerator) | &(r->denominator); | 
|---|
|  | 211 | t = simplify( &(r->numerator), &(r->denominator) ); | 
|---|
|  | 212 | r->numerator /= t; | 
|---|
|  | 213 | r->denominator /= t; | 
|---|
|  | 214 | return is; | 
|---|
|  | 215 | } // ?|? | 
|---|
|  | 216 |  | 
|---|
| [3d9b5da] | 217 | forall( dtype ostype | ostream( ostype ) ) | 
|---|
|  | 218 | ostype * ?|?( ostype *os, Rational r ) { | 
|---|
| [53ba273] | 219 | return os | r.numerator | '/' | r.denominator; | 
|---|
|  | 220 | } // ?|? | 
|---|
|  | 221 |  | 
|---|
|  | 222 | // Local Variables: // | 
|---|
|  | 223 | // tab-width: 4 // | 
|---|
|  | 224 | // End: // | 
|---|