Changes in / [2bdf50d:99ee64d]
- Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/examples/rational.c
r2bdf50d r99ee64d 11 11 // Created On : Mon Mar 28 08:43:12 2016 12 12 // Last Modified By : Peter A. Buhr 13 // Last Modified On : Fri Apr 8 11:27:48201614 // Update Count : 2 113 // Last Modified On : Wed May 4 14:19:36 2016 14 // Update Count : 24 15 15 // 16 16 … … 20 20 21 21 int main() { 22 Rational a, b, c;23 22 sout | "constructor" | endl; 24 a = rational( 3 ); 25 b = rational( 4 ); 26 c = rational(); 23 Rational a = { 3 }, b = { 4 }, c; 27 24 sout | a | b | c | endl; 28 a = rational( 4, 8 );29 b = rational( 5, 7 );25 a = (Rational){ 4, 8 }; 26 b = (Rational){ 5, 7 }; 30 27 sout | a | b | endl; 31 a = rational( -2, -3 );32 b = rational( 3, -2 );28 a = (Rational){ -2, -3 }; 29 b = (Rational){ 3, -2 }; 33 30 sout | a | b | endl; 34 a = rational( -2, 3 );35 b = rational( 3, 2 );31 a = (Rational){ -2, 3 }; 32 b = (Rational){ 3, 2 }; 36 33 sout | a | b | endl; 37 34 38 35 sout | "logical" | endl; 39 a = rational( -2 );40 b = rational( -3, 2 );36 a = (Rational){ -2 }; 37 b = (Rational){ -3, 2 }; 41 38 sout | a | b | endl; 42 39 sout | a == 1 | endl; … … 55 52 56 53 sout | "conversion" | endl; 57 a = rational( 3, 4 );54 a = (Rational){ 3, 4 }; 58 55 sout | widen( a ) | endl; 59 a = rational( 1, 7 );56 a = (Rational){ 1, 7 }; 60 57 sout | widen( a ) | endl; 61 a = rational( 355, 113 );58 a = (Rational){ 355, 113 }; 62 59 sout | widen( a ) | endl; 63 60 sout | narrow( 0.75, 4 ) | endl; … … 65 62 sout | narrow( 3.14159265358979, 256 ) | endl; 66 63 67 Rational x, y; 68 x = rational( 1, 2 ); 69 y = rational( 2 ); 64 Rational x = { 1, 2 }, y = { 2 }; 70 65 sout | x - y | endl; 71 66 sout | x > y | endl; … … 73 68 sout | y | denominator( y, -2 ) | y | endl; 74 69 75 Rational z; 76 z = rational( 0, 5 ); 70 Rational z = { 0, 5 }; 77 71 sout | z | endl; 78 72 79 73 sout | x | numerator( x, 0 ) | x | endl; 80 74 81 x = rational( 1, MAX ) + rational( 1, MAX );75 x = (Rational){ 1, MAX } + (Rational){ 1, MAX }; 82 76 sout | x | endl; 83 x = rational( 3, MAX ) + rational( 2, MAX );77 x = (Rational){ 3, MAX } + (Rational){ 2, MAX }; 84 78 sout | x | endl; 85 79 -
src/libcfa/rational
r2bdf50d r99ee64d 12 12 // Created On : Wed Apr 6 17:56:25 2016 13 13 // Last Modified By : Peter A. Buhr 14 // Last Modified On : Fri Apr 8 11:38:27201615 // Update Count : 1 514 // Last Modified On : Wed May 4 14:11:45 2016 15 // Update Count : 16 16 16 // 17 17 … … 28 28 29 29 // constructors 30 Rational rational();31 Rational rational(long int n );32 Rational rational(long int n, long int d );30 void ?{}( Rational * r ); 31 void ?{}( Rational * r, long int n ); 32 void ?{}( Rational * r, long int n, long int d ); 33 33 34 34 // getter/setter for numerator/denominator -
src/libcfa/rational.c
r2bdf50d r99ee64d 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 21 07:33:03201614 // Update Count : 2 213 // Last Modified On : Wed May 4 14:16:14 2016 14 // Update Count : 25 15 15 // 16 16 … … 53 53 // constructors 54 54 55 Rational rational() {56 r eturn (Rational){ 0, 1 };55 void ?{}( Rational * r ) { 56 r{ 0, 1 }; 57 57 } // rational 58 58 59 Rational rational(long int n ) {60 r eturn (Rational){ n, 1 };59 void ?{}( Rational * r, long int n ) { 60 r{ n, 1 }; 61 61 } // rational 62 62 63 Rational rational(long int n, long int d ) {63 void ?{}( Rational * r, long int n, long int d ) { 64 64 long int t = simplify( &n, &d ); // simplify 65 return (Rational){ n / t, d / t }; 65 r->numerator = n / t; 66 r->denominator = d / t; 66 67 } // rational 67 68 … … 172 173 Rational narrow( double f, long int md ) { 173 174 if ( md <= 1 ) { // maximum fractional digits too small? 174 Rational t = rational( f, 1 ); // truncate fraction 175 return t; 175 return (Rational){ f, 1}; // truncate fraction 176 176 } // if 177 177 … … 199 199 k[2] = x * k[1] + k[0]; k[0] = k[1]; k[1] = k[2]; 200 200 } // for 201 Rational t = rational( neg ? -h[1] : h[1], k[1] ); 202 return t; 201 return (Rational){ neg ? -h[1] : h[1], k[1] }; 203 202 } // narrow 204 203
Note: See TracChangeset
for help on using the changeset viewer.