[bb82c03] | 1 | // |
---|
[53ba273] | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
[bb82c03] | 6 | // |
---|
[630a82a] | 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. |
---|
[bb82c03] | 10 | // |
---|
[53ba273] | 11 | // Author : Peter A. Buhr |
---|
| 12 | // Created On : Wed Apr 6 17:56:25 2016 |
---|
| 13 | // Last Modified By : Peter A. Buhr |
---|
[d1ab5331] | 14 | // Last Modified On : Wed May 4 14:11:45 2016 |
---|
| 15 | // Update Count : 16 |
---|
[bb82c03] | 16 | // |
---|
[17e5e2b] | 17 | #ifndef RATIONAL_H |
---|
| 18 | #define RATIONAL_H |
---|
[53ba273] | 19 | |
---|
[3d9b5da] | 20 | #include "iostream" |
---|
[53ba273] | 21 | |
---|
[630a82a] | 22 | // implementation |
---|
[53ba273] | 23 | struct Rational { |
---|
| 24 | long int numerator, denominator; // invariant: denominator > 0 |
---|
| 25 | }; // Rational |
---|
| 26 | |
---|
[630a82a] | 27 | // constants |
---|
[53ba273] | 28 | extern struct Rational 0; |
---|
| 29 | extern struct Rational 1; |
---|
| 30 | |
---|
[630a82a] | 31 | // constructors |
---|
[d1ab5331] | 32 | void ?{}( Rational * r ); |
---|
| 33 | void ?{}( Rational * r, long int n ); |
---|
| 34 | void ?{}( Rational * r, long int n, long int d ); |
---|
[630a82a] | 35 | |
---|
| 36 | // getter/setter for numerator/denominator |
---|
[53ba273] | 37 | long int numerator( Rational r ); |
---|
| 38 | long int numerator( Rational r, long int n ); |
---|
[630a82a] | 39 | long int denominator( Rational r ); |
---|
[53ba273] | 40 | long int denominator( Rational r, long int d ); |
---|
[630a82a] | 41 | |
---|
| 42 | // comparison |
---|
[53ba273] | 43 | int ?==?( Rational l, Rational r ); |
---|
| 44 | int ?!=?( Rational l, Rational r ); |
---|
| 45 | int ?<?( Rational l, Rational r ); |
---|
| 46 | int ?<=?( Rational l, Rational r ); |
---|
| 47 | int ?>?( Rational l, Rational r ); |
---|
| 48 | int ?>=?( Rational l, Rational r ); |
---|
[630a82a] | 49 | |
---|
| 50 | // arithmetic |
---|
[53ba273] | 51 | Rational -?( Rational r ); |
---|
| 52 | Rational ?+?( Rational l, Rational r ); |
---|
| 53 | Rational ?-?( Rational l, Rational r ); |
---|
| 54 | Rational ?*?( Rational l, Rational r ); |
---|
| 55 | Rational ?/?( Rational l, Rational r ); |
---|
[630a82a] | 56 | |
---|
| 57 | // conversion |
---|
[53ba273] | 58 | double widen( Rational r ); |
---|
| 59 | Rational narrow( double f, long int md ); |
---|
[630a82a] | 60 | |
---|
| 61 | // I/O |
---|
[3d9b5da] | 62 | forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); |
---|
| 63 | forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational ); |
---|
[53ba273] | 64 | |
---|
[17e5e2b] | 65 | #endif // RATIONAL_H |
---|
| 66 | |
---|
[53ba273] | 67 | // Local Variables: // |
---|
| 68 | // mode: c // |
---|
| 69 | // tab-width: 4 // |
---|
| 70 | // End: // |
---|