Changeset df2be83 for src/libcfa
- Timestamp:
- Apr 11, 2016, 4:41:29 PM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 4b8f918
- Parents:
- 5ba653c (diff), e55ca05 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- src/libcfa
- Files:
-
- 2 edited
-
rational (modified) (3 diffs)
-
rational.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/rational
r5ba653c rdf2be83 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // rational -- 7 // rational -- Rational numbers are numbers written as a ratio, i.e., as a fraction, where the numerator (top number) 8 // and the denominator (bottom number) are whole numbers. When creating and computing with rational numbers, results 9 // are constantly reduced to keep the numerator and denominator as small as possible. 8 10 // 9 11 // Author : Peter A. Buhr 10 12 // Created On : Wed Apr 6 17:56:25 2016 11 13 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Apr 7 17:23:36201613 // Update Count : 914 // Last Modified On : Fri Apr 8 11:38:27 2016 15 // Update Count : 15 14 16 // 15 17 16 18 #include "iostream" 17 19 20 // implementation 18 21 struct Rational { 19 22 long int numerator, denominator; // invariant: denominator > 0 20 23 }; // Rational 21 24 25 // constants 22 26 extern struct Rational 0; 23 27 extern struct Rational 1; 24 28 25 long int gcd( long int a, long int b ); 26 long int simplify( long int *n, long int *d ); 27 Rational rational(); // constructor 28 Rational rational( long int n ); // constructor 29 Rational rational( long int n, long int d ); // constructor 29 // constructors 30 Rational rational(); 31 Rational rational( long int n ); 32 Rational rational( long int n, long int d ); 33 34 // getter/setter for numerator/denominator 30 35 long int numerator( Rational r ); 31 36 long int numerator( Rational r, long int n ); 37 long int denominator( Rational r ); 32 38 long int denominator( Rational r, long int d ); 39 40 // comparison 33 41 int ?==?( Rational l, Rational r ); 34 42 int ?!=?( Rational l, Rational r ); … … 37 45 int ?>?( Rational l, Rational r ); 38 46 int ?>=?( Rational l, Rational r ); 47 48 // arithmetic 39 49 Rational -?( Rational r ); 40 50 Rational ?+?( Rational l, Rational r ); … … 42 52 Rational ?*?( Rational l, Rational r ); 43 53 Rational ?/?( Rational l, Rational r ); 54 55 // conversion 44 56 double widen( Rational r ); 45 57 Rational narrow( double f, long int md ); 58 59 // I/O 46 60 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); 47 61 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational ); -
src/libcfa/rational.c
r5ba653c rdf2be83 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 17:35:05 2016 14 // Update Count : 18 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 }; 54 59 } // rational 55 60 56 Rational rational( long int n ) { // constructor 57 // r = (Rational){ n, 1 }; 58 Rational t = { n, 1 }; 59 return t; 61 Rational rational( long int n ) { 62 return (Rational){ n, 1 }; 60 63 } // rational 61 64 62 Rational rational( long int n, long int d ) { // constructor65 Rational rational( long int n, long int d ) { 63 66 long int t = simplify( &n, &d ); // simplify 64 // r = (Rational){ n / t, d / t }; 65 Rational t = { n / t, d / t }; 66 return t; 67 return (Rational){ n / t, d / t }; 67 68 } // rational 69 70 71 // getter/setter for numerator/denominator 68 72 69 73 long int numerator( Rational r ) { … … 79 83 } // numerator 80 84 85 long int denominator( Rational r ) { 86 return r.denominator; 87 } // denominator 88 81 89 long int denominator( Rational r, long int d ) { 82 90 long int prev = r.denominator; … … 87 95 } // denominator 88 96 97 98 // comparison 99 89 100 int ?==?( Rational l, Rational r ) { 90 101 return l.numerator * r.denominator == l.denominator * r.numerator; … … 110 121 return ! ( l < r ); 111 122 } // ?>=? 123 124 125 // arithmetic 112 126 113 127 Rational -?( Rational r ) { … … 149 163 return t; 150 164 } // ?/? 165 166 167 // conversion 151 168 152 169 double widen( Rational r ) { … … 188 205 } // narrow 189 206 207 208 // I/O 209 190 210 forall( dtype istype | istream( istype ) ) 191 211 istype * ?|?( istype *is, Rational *r ) {
Note:
See TracChangeset
for help on using the changeset viewer.