Changes in src/libcfa/rational [630a82a:3d9b5da]
- File:
-
- 1 edited
-
src/libcfa/rational (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/rational
r630a82a r3d9b5da 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 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. 7 // rational -- 10 8 // 11 9 // Author : Peter A. Buhr 12 10 // Created On : Wed Apr 6 17:56:25 2016 13 11 // Last Modified By : Peter A. Buhr 14 // Last Modified On : Fri Apr 8 11:38:27201615 // Update Count : 1512 // Last Modified On : Thu Apr 7 17:23:36 2016 13 // Update Count : 9 16 14 // 17 15 18 16 #include "iostream" 19 17 20 // implementation21 18 struct Rational { 22 19 long int numerator, denominator; // invariant: denominator > 0 23 20 }; // Rational 24 21 25 // constants26 22 extern struct Rational 0; 27 23 extern struct Rational 1; 28 24 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 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 35 30 long int numerator( Rational r ); 36 31 long int numerator( Rational r, long int n ); 37 long int denominator( Rational r );38 32 long int denominator( Rational r, long int d ); 39 40 // comparison41 33 int ?==?( Rational l, Rational r ); 42 34 int ?!=?( Rational l, Rational r ); … … 45 37 int ?>?( Rational l, Rational r ); 46 38 int ?>=?( Rational l, Rational r ); 47 48 // arithmetic49 39 Rational -?( Rational r ); 50 40 Rational ?+?( Rational l, Rational r ); … … 52 42 Rational ?*?( Rational l, Rational r ); 53 43 Rational ?/?( Rational l, Rational r ); 54 55 // conversion56 44 double widen( Rational r ); 57 45 Rational narrow( double f, long int md ); 58 59 // I/O60 46 forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); 61 47 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
Note:
See TracChangeset
for help on using the changeset viewer.