Changes in src/libcfa/rational.c [3d9b5da:630a82a]
- File:
-
- 1 edited
-
src/libcfa/rational.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/rational.c
r3d9b5da r630a82a 11 11 // Created On : Wed Apr 6 17:54:28 2016 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Thu Apr 7 17:28:03201614 // Update Count : 1 213 // Last Modified On : Fri Apr 8 15:39:17 2016 14 // Update Count : 17 15 15 // 16 16 … … 23 23 } // extern 24 24 25 26 // constants 27 25 28 struct Rational 0 = {0, 1}; 26 29 struct Rational 1 = {1, 1}; 27 30 28 // Calculate the greatest common denominator of two numbers, the first of which may be negative. It is used to reduce 29 // rationals. 30 31 long int gcd( long int a, long int b ) { 31 32 // helper 33 34 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals. 35 static long int gcd( long int a, long int b ) { 32 36 for ( ;; ) { // Euclid's algorithm 33 37 long int r = a % b; … … 39 43 } // gcd 40 44 41 long int simplify( long int *n, long int *d ) {45 static long int simplify( long int *n, long int *d ) { 42 46 if ( *d == 0 ) { 43 47 serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl; … … 48 52 } // Rationalnumber::simplify 49 53 50 Rational rational() { // constructor 51 // r = (Rational){ 0, 1 }; 52 Rational t = { 0, 1 }; 53 return t; 54 55 // constructors 56 57 Rational rational() { 58 return (Rational){ 0, 1 }; 59 // Rational t = { 0, 1 }; 60 // return t; 54 61 } // rational 55 62 56 Rational rational( long int n ) { // constructor57 // r =(Rational){ n, 1 };58 Rational t = { n, 1 };59 return t;63 Rational rational( long int n ) { 64 return (Rational){ n, 1 }; 65 // Rational t = { n, 1 }; 66 // return t; 60 67 } // rational 61 68 62 Rational rational( long int n, long int d ) { // constructor69 Rational rational( long int n, long int d ) { 63 70 long int t = simplify( &n, &d ); // simplify 64 71 // r = (Rational){ n / t, d / t }; … … 66 73 return t; 67 74 } // rational 75 76 77 // getter/setter for numerator/denominator 68 78 69 79 long int numerator( Rational r ) { … … 79 89 } // numerator 80 90 91 long int denominator( Rational r ) { 92 return r.denominator; 93 } // denominator 94 81 95 long int denominator( Rational r, long int d ) { 82 96 long int prev = r.denominator; … … 87 101 } // denominator 88 102 103 104 // comparison 105 89 106 int ?==?( Rational l, Rational r ) { 90 107 return l.numerator * r.denominator == l.denominator * r.numerator; … … 110 127 return ! ( l < r ); 111 128 } // ?>=? 129 130 131 // arithmetic 112 132 113 133 Rational -?( Rational r ) { … … 149 169 return t; 150 170 } // ?/? 171 172 173 // conversion 151 174 152 175 double widen( Rational r ) { … … 188 211 } // narrow 189 212 213 214 // I/O 215 190 216 forall( dtype istype | istream( istype ) ) 191 217 istype * ?|?( istype *is, Rational *r ) {
Note:
See TracChangeset
for help on using the changeset viewer.