Changeset f621a148 for src/libcfa/rational.c
- Timestamp:
- May 1, 2017, 12:38:10 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 13e2c54
- Parents:
- c07d724
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/rational.c
rc07d724 rf621a148 10 10 // Created On : Wed Apr 6 17:54:28 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Jul 9 11:18:04 201613 // Update Count : 4012 // Last Modified On : Thu Apr 27 17:05:06 2017 13 // Update Count : 51 14 14 // 15 15 … … 30 30 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals. 31 31 // alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm 32 static long int gcd( long int a, long intb ) {32 static RationalImpl gcd( RationalImpl a, RationalImpl b ) { 33 33 for ( ;; ) { // Euclid's algorithm 34 long intr = a % b;34 RationalImpl r = a % b; 35 35 if ( r == 0 ) break; 36 36 a = b; … … 40 40 } // gcd 41 41 42 static long int simplify( long int *n, long int*d ) {42 static RationalImpl simplify( RationalImpl *n, RationalImpl *d ) { 43 43 if ( *d == 0 ) { 44 44 serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl; … … 56 56 } // rational 57 57 58 void ?{}( Rational * r, long intn ) {58 void ?{}( Rational * r, RationalImpl n ) { 59 59 r{ n, 1 }; 60 60 } // rational 61 61 62 void ?{}( Rational * r, long int n, long intd ) {63 long int t = simplify( &n, &d );// simplify62 void ?{}( Rational * r, RationalImpl n, RationalImpl d ) { 63 RationalImpl t = simplify( &n, &d ); // simplify 64 64 r->numerator = n / t; 65 65 r->denominator = d / t; … … 67 67 68 68 69 // getter /setterfor numerator/denominator70 71 long intnumerator( Rational r ) {69 // getter for numerator/denominator 70 71 RationalImpl numerator( Rational r ) { 72 72 return r.numerator; 73 73 } // numerator 74 74 75 long int numerator( Rational r, long int n ) { 76 long int prev = r.numerator; 77 long int t = gcd( abs( n ), r.denominator ); // simplify 75 RationalImpl denominator( Rational r ) { 76 return r.denominator; 77 } // denominator 78 79 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src ) { 80 return *dest = src.[ numerator, denominator ]; 81 } 82 83 // setter for numerator/denominator 84 85 RationalImpl numerator( Rational r, RationalImpl n ) { 86 RationalImpl prev = r.numerator; 87 RationalImpl t = gcd( abs( n ), r.denominator ); // simplify 78 88 r.numerator = n / t; 79 89 r.denominator = r.denominator / t; … … 81 91 } // numerator 82 92 83 long int denominator( Rational r ) { 84 return r.denominator; 85 } // denominator 86 87 long int denominator( Rational r, long int d ) { 88 long int prev = r.denominator; 89 long int t = simplify( &r.numerator, &d ); // simplify 93 RationalImpl denominator( Rational r, RationalImpl d ) { 94 RationalImpl prev = r.denominator; 95 RationalImpl t = simplify( &r.numerator, &d ); // simplify 90 96 r.numerator = r.numerator / t; 91 97 r.denominator = d / t; … … 170 176 171 177 // http://www.ics.uci.edu/~eppstein/numth/frap.c 172 Rational narrow( double f, long intmd ) {178 Rational narrow( double f, RationalImpl md ) { 173 179 if ( md <= 1 ) { // maximum fractional digits too small? 174 180 return (Rational){ f, 1}; // truncate fraction … … 176 182 177 183 // continued fraction coefficients 178 long intm00 = 1, m11 = 1, m01 = 0, m10 = 0;179 long intai, t;184 RationalImpl m00 = 1, m11 = 1, m01 = 0, m10 = 0; 185 RationalImpl ai, t; 180 186 181 187 // find terms until denom gets too big 182 188 for ( ;; ) { 183 ai = ( long int)f;189 ai = (RationalImpl)f; 184 190 if ( ! (m10 * ai + m11 <= md) ) break; 185 191 t = m00 * ai + m01; … … 202 208 forall( dtype istype | istream( istype ) ) 203 209 istype * ?|?( istype *is, Rational *r ) { 204 long intt;210 RationalImpl t; 205 211 is | &(r->numerator) | &(r->denominator); 206 212 t = simplify( &(r->numerator), &(r->denominator) );
Note: See TracChangeset
for help on using the changeset viewer.