Changeset 561f730
- Timestamp:
- May 14, 2017, 6:30:20 PM (8 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:
- a32cfc90
- Parents:
- 4c8f86b3
- Location:
- src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/rational
r4c8f86b3 r561f730 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:06201715 // Update Count : 3314 // Last Modified On : Sun May 14 16:49:13 2017 15 // Update Count : 78 16 16 // 17 17 … … 21 21 #include "iostream" 22 22 23 trait scalar( otype T ) { 24 }; 25 26 trait arithmetic( otype T | scalar( T ) ) { 27 int !?( T ); 28 int ?==?( T, T ); 29 int ?!=?( T, T ); 30 int ?<?( T, T ); 31 int ?<=?( T, T ); 32 int ?>?( T, T ); 33 int ?>=?( T, T ); 34 void ?{}( T *, zero_t ); 35 void ?{}( T *, one_t ); 36 T +?( T ); 37 T -?( T ); 38 T ?+?( T, T ); 39 T ?-?( T, T ); 40 T ?*?( T, T ); 41 T ?/?( T, T ); 42 T ?%?( T, T ); 43 T ?/=?( T *, T ); 44 T abs( T ); 45 }; 46 23 47 // implementation 24 typedef long int RationalImpl; 48 49 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 25 50 struct Rational { 26 RationalImpl numerator, denominator; 51 RationalImpl numerator, denominator; // invariant: denominator > 0 27 52 }; // Rational 28 53 29 // constants 30 extern struct Rational 0; 31 extern struct Rational 1; 54 // constructors 32 55 33 // constructors 34 void ?{}( Rational * r ); 35 void ?{}( Rational * r, RationalImpl n ); 36 void ?{}( Rational * r, RationalImpl n, RationalImpl d ); 56 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 57 void ?{}( Rational(RationalImpl) * r ); 58 59 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 60 void ?{}( Rational(RationalImpl) * r, RationalImpl n ); 61 62 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 63 void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d ); 64 65 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 66 void ?{}( Rational(RationalImpl) * r, zero_t ); 67 68 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 69 void ?{}( Rational(RationalImpl) * r, one_t ); 37 70 38 71 // getter for numerator/denominator 39 RationalImpl numerator( Rational r ); 40 RationalImpl denominator( Rational r ); 41 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src ); 72 73 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 74 RationalImpl numerator( Rational(RationalImpl) r ); 75 76 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 77 RationalImpl denominator( Rational(RationalImpl) r ); 78 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 79 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ); 80 42 81 // setter for numerator/denominator 43 RationalImpl numerator( Rational r, RationalImpl n ); 44 RationalImpl denominator( Rational r, RationalImpl d ); 82 83 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 84 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ); 85 86 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 87 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ); 45 88 46 89 // comparison 47 int ?==?( Rational l, Rational r ); 48 int ?!=?( Rational l, Rational r ); 49 int ?<?( Rational l, Rational r ); 50 int ?<=?( Rational l, Rational r ); 51 int ?>?( Rational l, Rational r ); 52 int ?>=?( Rational l, Rational r ); 90 91 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 92 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 93 94 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 95 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 96 97 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 98 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 99 100 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 101 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 102 103 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 104 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 105 106 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 107 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 53 108 54 109 // arithmetic 55 Rational -?( Rational r ); 56 Rational ?+?( Rational l, Rational r ); 57 Rational ?-?( Rational l, Rational r ); 58 Rational ?*?( Rational l, Rational r ); 59 Rational ?/?( Rational l, Rational r ); 110 111 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 112 Rational(RationalImpl) +?( Rational(RationalImpl) r ); 113 114 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 115 Rational(RationalImpl) -?( Rational(RationalImpl) r ); 116 117 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 118 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 119 120 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 121 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 122 123 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 124 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 125 126 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 127 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 60 128 61 129 // conversion 62 double widen( Rational r ); 63 Rational narrow( double f, RationalImpl md ); 130 // forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 131 // double widen( Rational(RationalImpl) r ); 132 // forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 133 // Rational(RationalImpl) narrow( double f, RationalImpl md ); 64 134 65 135 // I/O 66 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); 67 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational ); 136 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 137 forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } ) 138 istype * ?|?( istype *, Rational(RationalImpl) * ); 139 140 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 141 forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } ) 142 ostype * ?|?( ostype *, Rational(RationalImpl ) ); 68 143 69 144 #endif // RATIONAL_H -
src/libcfa/rational.c
r4c8f86b3 r561f730 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:06201713 // Update Count : 5112 // Last Modified On : Sun May 14 17:25:19 2017 13 // Update Count : 131 14 14 // 15 15 … … 17 17 #include "fstream" 18 18 #include "stdlib" 19 #include "math" // floor20 21 22 // constants23 24 struct Rational 0 = {0, 1};25 struct Rational 1 = {1, 1};26 27 19 28 20 // helper routines … … 30 22 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals. 31 23 // alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm 24 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 32 25 static RationalImpl gcd( RationalImpl a, RationalImpl b ) { 33 26 for ( ;; ) { // Euclid's algorithm 34 27 RationalImpl r = a % b; 35 if ( r == 0) break;28 if ( r == (RationalImpl){0} ) break; 36 29 a = b; 37 30 b = r; … … 40 33 } // gcd 41 34 42 static RationalImpl simplify( RationalImpl *n, RationalImpl *d ) { 43 if ( *d == 0 ) { 35 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 36 static RationalImpl simplify( RationalImpl * n, RationalImpl * d ) { 37 if ( *d == (RationalImpl){0} ) { 44 38 serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl; 45 39 exit( EXIT_FAILURE ); 46 40 } // exit 47 if ( *d < 0 ) { *d = -*d; *n = -*n; }// move sign to numerator41 if ( *d < (RationalImpl){0} ) { *d = -*d; *n = -*n; } // move sign to numerator 48 42 return gcd( abs( *n ), *d ); // simplify 49 43 } // Rationalnumber::simplify … … 52 46 // constructors 53 47 54 void ?{}( Rational * r ) { 55 r{ 0, 1 }; 48 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 49 void ?{}( Rational(RationalImpl) * r ) { 50 r{ (RationalImpl){0}, (RationalImpl){1} }; 56 51 } // rational 57 52 58 void ?{}( Rational * r, RationalImpl n ) { 59 r{ n, 1 }; 53 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 54 void ?{}( Rational(RationalImpl) * r, RationalImpl n ) { 55 r{ n, (RationalImpl){1} }; 60 56 } // rational 61 57 62 void ?{}( Rational * r, RationalImpl n, RationalImpl d ) { 58 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 59 void ?{}( Rational(RationalImpl) * r, RationalImpl n, RationalImpl d ) { 63 60 RationalImpl t = simplify( &n, &d ); // simplify 64 61 r->numerator = n / t; … … 69 66 // getter for numerator/denominator 70 67 71 RationalImpl numerator( Rational r ) { 68 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 69 RationalImpl numerator( Rational(RationalImpl) r ) { 72 70 return r.numerator; 73 71 } // numerator 74 72 75 RationalImpl denominator( Rational r ) { 73 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 74 RationalImpl denominator( Rational(RationalImpl) r ) { 76 75 return r.denominator; 77 76 } // denominator 78 77 79 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src ) { 78 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 79 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) { 80 80 return *dest = src.[ numerator, denominator ]; 81 81 } … … 83 83 // setter for numerator/denominator 84 84 85 RationalImpl numerator( Rational r, RationalImpl n ) { 85 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 86 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) { 86 87 RationalImpl prev = r.numerator; 87 88 RationalImpl t = gcd( abs( n ), r.denominator ); // simplify … … 91 92 } // numerator 92 93 93 RationalImpl denominator( Rational r, RationalImpl d ) { 94 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 95 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) { 94 96 RationalImpl prev = r.denominator; 95 97 RationalImpl t = simplify( &r.numerator, &d ); // simplify … … 102 104 // comparison 103 105 104 int ?==?( Rational l, Rational r ) { 106 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 107 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 105 108 return l.numerator * r.denominator == l.denominator * r.numerator; 106 109 } // ?==? 107 110 108 int ?!=?( Rational l, Rational r ) { 111 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 112 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 109 113 return ! ( l == r ); 110 114 } // ?!=? 111 115 112 int ?<?( Rational l, Rational r ) { 116 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 117 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 113 118 return l.numerator * r.denominator < l.denominator * r.numerator; 114 119 } // ?<? 115 120 116 int ?<=?( Rational l, Rational r ) { 117 return l < r || l == r; 121 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 122 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 123 return l.numerator * r.denominator <= l.denominator * r.numerator; 118 124 } // ?<=? 119 125 120 int ?>?( Rational l, Rational r ) { 126 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 127 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 121 128 return ! ( l <= r ); 122 129 } // ?>? 123 130 124 int ?>=?( Rational l, Rational r ) { 131 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 132 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 125 133 return ! ( l < r ); 126 134 } // ?>=? … … 129 137 // arithmetic 130 138 131 Rational -?( Rational r ) { 132 Rational t = { -r.numerator, r.denominator }; 139 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 140 Rational(RationalImpl) +?( Rational(RationalImpl) r ) { 141 Rational(RationalImpl) t = { r.numerator, r.denominator }; 142 return t; 143 } // +? 144 145 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 146 Rational(RationalImpl) -?( Rational(RationalImpl) r ) { 147 Rational(RationalImpl) t = { -r.numerator, r.denominator }; 133 148 return t; 134 149 } // -? 135 150 136 Rational ?+?( Rational l, Rational r ) { 151 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 152 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 137 153 if ( l.denominator == r.denominator ) { // special case 138 Rational t = { l.numerator + r.numerator, l.denominator };154 Rational(RationalImpl) t = { l.numerator + r.numerator, l.denominator }; 139 155 return t; 140 156 } else { 141 Rational t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };157 Rational(RationalImpl) t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; 142 158 return t; 143 159 } // if 144 160 } // ?+? 145 161 146 Rational ?-?( Rational l, Rational r ) { 162 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 163 Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 147 164 if ( l.denominator == r.denominator ) { // special case 148 Rational t = { l.numerator - r.numerator, l.denominator };165 Rational(RationalImpl) t = { l.numerator - r.numerator, l.denominator }; 149 166 return t; 150 167 } else { 151 Rational t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };168 Rational(RationalImpl) t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; 152 169 return t; 153 170 } // if 154 171 } // ?-? 155 172 156 Rational ?*?( Rational l, Rational r ) { 157 Rational t = { l.numerator * r.numerator, l.denominator * r.denominator }; 173 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 174 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 175 Rational(RationalImpl) t = { l.numerator * r.numerator, l.denominator * r.denominator }; 158 176 return t; 159 177 } // ?*? 160 178 161 Rational ?/?( Rational l, Rational r ) { 162 if ( r.numerator < 0 ) { 179 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 180 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 181 if ( r.numerator < (RationalImpl){0} ) { 163 182 r.numerator = -r.numerator; 164 183 r.denominator = -r.denominator; 165 184 } // if 166 Rational t = { l.numerator * r.denominator, l.denominator * r.numerator };185 Rational(RationalImpl) t = { l.numerator * r.denominator, l.denominator * r.numerator }; 167 186 return t; 168 187 } // ?/? … … 171 190 // conversion 172 191 173 double widen( Rational r ) { 174 return (double)r.numerator / (double)r.denominator; 175 } // widen 176 177 // http://www.ics.uci.edu/~eppstein/numth/frap.c 178 Rational narrow( double f, RationalImpl md ) { 179 if ( md <= 1 ) { // maximum fractional digits too small? 180 return (Rational){ f, 1}; // truncate fraction 181 } // if 182 183 // continued fraction coefficients 184 RationalImpl m00 = 1, m11 = 1, m01 = 0, m10 = 0; 185 RationalImpl ai, t; 186 187 // find terms until denom gets too big 188 for ( ;; ) { 189 ai = (RationalImpl)f; 190 if ( ! (m10 * ai + m11 <= md) ) break; 191 t = m00 * ai + m01; 192 m01 = m00; 193 m00 = t; 194 t = m10 * ai + m11; 195 m11 = m10; 196 m10 = t; 197 t = (double)ai; 198 if ( f == t ) break; // prevent division by zero 199 f = 1 / (f - t); 200 if ( f > (double)0x7FFFFFFF ) break; // representation failure 201 } 202 return (Rational){ m00, m10 }; 203 } // narrow 192 // forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 193 // double widen( Rational(RationalImpl) r ) { 194 // return (double)r.numerator / (double)r.denominator; 195 // } // widen 196 197 // // http://www.ics.uci.edu/~eppstein/numth/frap.c 198 // forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 199 // Rational(RationalImpl) narrow( double f, RationalImpl md ) { 200 // if ( md <= 1 ) { // maximum fractional digits too small? 201 // return (Rational(RationalImpl)){ f, 1}; // truncate fraction 202 // } // if 203 204 // // continued fraction coefficients 205 // RationalImpl m00 = 1, m11 = 1, m01 = 0, m10 = 0; 206 // RationalImpl ai, t; 207 208 // // find terms until denom gets too big 209 // for ( ;; ) { 210 // ai = (RationalImpl)f; 211 // if ( ! (m10 * ai + m11 <= md) ) break; 212 // t = m00 * ai + m01; 213 // m01 = m00; 214 // m00 = t; 215 // t = m10 * ai + m11; 216 // m11 = m10; 217 // m10 = t; 218 // t = (double)ai; 219 // if ( f == t ) break; // prevent division by zero 220 // f = 1 / (f - (double)t); 221 // if ( f > (double)0x7FFFFFFF ) break; // representation failure 222 // } 223 // return (Rational(RationalImpl)){ m00, m10 }; 224 // } // narrow 204 225 205 226 206 227 // I/O 207 228 208 forall( dtype istype | istream( istype ) ) 209 istype * ?|?( istype *is, Rational *r ) { 229 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 230 forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } ) 231 istype * ?|?( istype * is, Rational(RationalImpl) * r ) { 210 232 RationalImpl t; 211 233 is | &(r->numerator) | &(r->denominator); … … 216 238 } // ?|? 217 239 218 forall( dtype ostype | ostream( ostype ) ) 219 ostype * ?|?( ostype *os, Rational r ) { 240 forall ( otype RationalImpl | arithmetic( RationalImpl ) ) 241 forall( dtype ostype | ostream( ostype ) | { ostype * ?|?( ostype *, RationalImpl ); } ) 242 ostype * ?|?( ostype * os, Rational(RationalImpl ) r ) { 220 243 return os | r.numerator | '/' | r.denominator; 221 244 } // ?|? -
src/tests/.expect/rational.txt
r4c8f86b3 r561f730 17 17 3/1 18 18 4/3 19 conversion20 0.7521 0.14285714285714322 3.1415929203539823 3/424 1/725 355/11326 19 decompose 27 20 more tests -
src/tests/rational.c
r4c8f86b3 r561f730 10 10 // Created On : Mon Mar 28 08:43:12 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 2 22:11:05201713 // Update Count : 4112 // Last Modified On : Sun May 14 18:10:28 2017 13 // Update Count : 57 14 14 // 15 15 16 #include <rational> 16 17 #include <limits> 17 #include < rational>18 #include <stdlib> 18 19 #include <fstream> 20 21 // UNNECESSARY, FIX ME 22 void ?{}( int * this ) { *this = 0; } 23 void ?{}( int * this, zero_t ) { *this = 0; } 24 void ?{}( int * this, one_t ) { *this = 1; } 19 25 20 26 int main() { 21 27 sout | "constructor" | endl; 22 Rational a = { 3 }, b = { 4 }, c;28 Rational(int) a = { 3 }, b = { 4 }, c; 23 29 sout | a | b | c | endl; 24 a = (Rational){ 4, 8 }; 25 b = (Rational){ 5, 7 }; 30 31 a = (Rational(int)){ 4, 8 }; 32 b = (Rational(int)){ 5, 7 }; 26 33 sout | a | b | endl; 27 a = (Rational ){ -2, -3 };28 b = (Rational ){ 3, -2 };34 a = (Rational(int)){ -2, -3 }; 35 b = (Rational(int)){ 3, -2 }; 29 36 sout | a | b | endl; 30 a = (Rational ){ -2, 3 };31 b = (Rational ){ 3, 2 };37 a = (Rational(int)){ -2, 3 }; 38 b = (Rational(int)){ 3, 2 }; 32 39 sout | a | b | endl; 33 40 34 41 sout | "logical" | endl; 35 a = (Rational ){ -2 };36 b = (Rational ){ -3, 2 };42 a = (Rational(int)){ -2 }; 43 b = (Rational(int)){ -3, 2 }; 37 44 sout | a | b | endl; 38 45 // sout | a == 1 | endl; // FIX ME … … 50 57 sout | a / b | endl; 51 58 52 sout | "conversion" | endl;53 a = (Rational){ 3, 4 };54 sout | widen( a ) | endl;55 a = (Rational){ 1, 7 };56 sout | widen( a ) | endl;57 a = (Rational){ 355, 113 };58 sout | widen( a ) | endl;59 sout | narrow( 0.75, 4 ) | endl;60 sout | narrow( 0.14285714285714, 16 ) | endl;61 sout | narrow( 3.14159265358979, 256 ) | endl;59 // sout | "conversion" | endl; 60 // a = (Rational(int)){ 3, 4 }; 61 // sout | widen( a ) | endl; 62 // a = (Rational(int)){ 1, 7 }; 63 // sout | widen( a ) | endl; 64 // a = (Rational(int)){ 355, 113 }; 65 // sout | widen( a ) | endl; 66 // sout | narrow( 0.75, 4 ) | endl; 67 // sout | narrow( 0.14285714285714, 16 ) | endl; 68 // sout | narrow( 3.14159265358979, 256 ) | endl; 62 69 63 70 sout | "decompose" | endl; 64 RationalImpln, d;71 int n, d; 65 72 // [n, d] = a; 66 73 // sout | a | n | d | endl; 67 74 68 75 sout | "more tests" | endl; 69 Rational x = { 1, 2 }, y = { 2 };76 Rational(int) x = { 1, 2 }, y = { 2 }; 70 77 sout | x - y | endl; 71 78 sout | x > y | endl; … … 73 80 sout | y | denominator( y, -2 ) | y | endl; 74 81 75 Rational z = { 0, 5 };82 Rational(int) z = { 0, 5 }; 76 83 sout | z | endl; 77 84 78 85 sout | x | numerator( x, 0 ) | x | endl; 79 86 80 x = (Rational ){ 1, MAX } + (Rational){ 1, MAX };87 x = (Rational(int)){ 1, MAX } + (Rational(int)){ 1, MAX }; 81 88 sout | x | endl; 82 x = (Rational ){ 3, MAX } + (Rational){ 2, MAX };89 x = (Rational(int)){ 3, MAX } + (Rational(int)){ 2, MAX }; 83 90 sout | x | endl; 84 91
Note: See TracChangeset
for help on using the changeset viewer.