Changes in / [13e2c54:0428aad]
- Location:
- src
- Files:
-
- 2 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/rational
r13e2c54 r0428aad 12 12 // Created On : Wed Apr 6 17:56:25 2016 13 13 // Last Modified By : Peter A. Buhr 14 // Last Modified On : Mon May 1 08:25:06 201715 // Update Count : 3314 // Last Modified On : Wed May 4 14:11:45 2016 15 // Update Count : 16 16 16 // 17 18 17 #ifndef RATIONAL_H 19 18 #define RATIONAL_H … … 22 21 23 22 // implementation 24 typedef long int RationalImpl;25 23 struct Rational { 26 RationalImplnumerator, denominator; // invariant: denominator > 024 long int numerator, denominator; // invariant: denominator > 0 27 25 }; // Rational 28 26 … … 33 31 // constructors 34 32 void ?{}( Rational * r ); 35 void ?{}( Rational * r, RationalImpln );36 void ?{}( Rational * r, RationalImpl n, RationalImpld );33 void ?{}( Rational * r, long int n ); 34 void ?{}( Rational * r, long int n, long int d ); 37 35 38 // getter for numerator/denominator 39 RationalImpl numerator( Rational r ); 40 RationalImpl denominator( Rational r ); 41 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src ); 42 // setter for numerator/denominator 43 RationalImpl numerator( Rational r, RationalImpl n ); 44 RationalImpl denominator( Rational r, RationalImpl d ); 36 // getter/setter for numerator/denominator 37 long int numerator( Rational r ); 38 long int numerator( Rational r, long int n ); 39 long int denominator( Rational r ); 40 long int denominator( Rational r, long int d ); 45 41 46 42 // comparison … … 61 57 // conversion 62 58 double widen( Rational r ); 63 Rational narrow( double f, RationalImplmd );59 Rational narrow( double f, long int md ); 64 60 65 61 // I/O -
src/libcfa/rational.c
r13e2c54 r0428aad 10 10 // Created On : Wed Apr 6 17:54:28 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Apr 27 17:05:06 201713 // Update Count : 5112 // Last Modified On : Sat Jul 9 11:18:04 2016 13 // Update Count : 40 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 RationalImpl gcd( RationalImpl a, RationalImplb ) {32 static long int gcd( long int a, long int b ) { 33 33 for ( ;; ) { // Euclid's algorithm 34 RationalImplr = a % b;34 long int r = a % b; 35 35 if ( r == 0 ) break; 36 36 a = b; … … 40 40 } // gcd 41 41 42 static RationalImpl simplify( RationalImpl *n, RationalImpl*d ) {42 static long int simplify( long int *n, long int *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, RationalImpln ) {58 void ?{}( Rational * r, long int n ) { 59 59 r{ n, 1 }; 60 60 } // rational 61 61 62 void ?{}( Rational * r, RationalImpl n, RationalImpld ) {63 RationalImpl t = simplify( &n, &d );// simplify62 void ?{}( Rational * r, long int n, long int d ) { 63 long int t = simplify( &n, &d ); // simplify 64 64 r->numerator = n / t; 65 65 r->denominator = d / t; … … 67 67 68 68 69 // getter for numerator/denominator70 71 RationalImplnumerator( Rational r ) {69 // getter/setter for numerator/denominator 70 71 long int numerator( Rational r ) { 72 72 return r.numerator; 73 73 } // numerator 74 74 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 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 88 78 r.numerator = n / t; 89 79 r.denominator = r.denominator / t; … … 91 81 } // numerator 92 82 93 RationalImpl denominator( Rational r, RationalImpl d ) { 94 RationalImpl prev = r.denominator; 95 RationalImpl t = simplify( &r.numerator, &d ); // simplify 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 96 90 r.numerator = r.numerator / t; 97 91 r.denominator = d / t; … … 176 170 177 171 // http://www.ics.uci.edu/~eppstein/numth/frap.c 178 Rational narrow( double f, RationalImplmd ) {172 Rational narrow( double f, long int md ) { 179 173 if ( md <= 1 ) { // maximum fractional digits too small? 180 174 return (Rational){ f, 1}; // truncate fraction … … 182 176 183 177 // continued fraction coefficients 184 RationalImplm00 = 1, m11 = 1, m01 = 0, m10 = 0;185 RationalImplai, t;178 long int m00 = 1, m11 = 1, m01 = 0, m10 = 0; 179 long int ai, t; 186 180 187 181 // find terms until denom gets too big 188 182 for ( ;; ) { 189 ai = ( RationalImpl)f;183 ai = (long int)f; 190 184 if ( ! (m10 * ai + m11 <= md) ) break; 191 185 t = m00 * ai + m01; … … 208 202 forall( dtype istype | istream( istype ) ) 209 203 istype * ?|?( istype *is, Rational *r ) { 210 RationalImplt;204 long int t; 211 205 is | &(r->numerator) | &(r->denominator); 212 206 t = simplify( &(r->numerator), &(r->denominator) ); -
src/tests/rational.c
r13e2c54 r0428aad 10 10 // Created On : Mon Mar 28 08:43:12 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T hu Apr 27 17:05:19 201713 // Update Count : 4012 // Last Modified On : Tue Jul 5 18:29:37 2016 13 // Update Count : 25 14 14 // 15 15 … … 36 36 b = (Rational){ -3, 2 }; 37 37 sout | a | b | endl; 38 // sout | a == 1 | endl; // FIX ME 38 sout | a == 1 | endl; 39 39 sout | a != b | endl; 40 40 sout | a < b | endl; … … 61 61 sout | narrow( 3.14159265358979, 256 ) | endl; 62 62 63 sout | "decompose" | endl;64 RationalImpl n, d;65 [n, d] = a;66 sout | a | n | d | endl;67 68 sout | "more tests" | endl;69 63 Rational x = { 1, 2 }, y = { 2 }; 70 64 sout | x - y | endl;
Note: See TracChangeset
for help on using the changeset viewer.