[53ba273] | 1 | //
|
---|
| 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.
|
---|
| 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.
|
---|
[53ba273] | 10 | //
|
---|
| 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
|
---|
[53ba273] | 16 | //
|
---|
[17e5e2b] | 17 | #ifdef __CFORALL__
|
---|
| 18 |
|
---|
| 19 | #ifndef RATIONAL_H
|
---|
| 20 | #define RATIONAL_H
|
---|
[53ba273] | 21 |
|
---|
[3d9b5da] | 22 | #include "iostream"
|
---|
[53ba273] | 23 |
|
---|
[630a82a] | 24 | // implementation
|
---|
[53ba273] | 25 | struct Rational {
|
---|
| 26 | long int numerator, denominator; // invariant: denominator > 0
|
---|
| 27 | }; // Rational
|
---|
| 28 |
|
---|
[630a82a] | 29 | // constants
|
---|
[53ba273] | 30 | extern struct Rational 0;
|
---|
| 31 | extern struct Rational 1;
|
---|
| 32 |
|
---|
[630a82a] | 33 | // constructors
|
---|
[d1ab5331] | 34 | void ?{}( Rational * r );
|
---|
| 35 | void ?{}( Rational * r, long int n );
|
---|
| 36 | void ?{}( Rational * r, long int n, long int d );
|
---|
[630a82a] | 37 |
|
---|
| 38 | // getter/setter for numerator/denominator
|
---|
[53ba273] | 39 | long int numerator( Rational r );
|
---|
| 40 | long int numerator( Rational r, long int n );
|
---|
[630a82a] | 41 | long int denominator( Rational r );
|
---|
[53ba273] | 42 | long int denominator( Rational r, long int d );
|
---|
[630a82a] | 43 |
|
---|
| 44 | // comparison
|
---|
[53ba273] | 45 | int ?==?( Rational l, Rational r );
|
---|
| 46 | int ?!=?( Rational l, Rational r );
|
---|
| 47 | int ?<?( Rational l, Rational r );
|
---|
| 48 | int ?<=?( Rational l, Rational r );
|
---|
| 49 | int ?>?( Rational l, Rational r );
|
---|
| 50 | int ?>=?( Rational l, Rational r );
|
---|
[630a82a] | 51 |
|
---|
| 52 | // arithmetic
|
---|
[53ba273] | 53 | Rational -?( Rational r );
|
---|
| 54 | Rational ?+?( Rational l, Rational r );
|
---|
| 55 | Rational ?-?( Rational l, Rational r );
|
---|
| 56 | Rational ?*?( Rational l, Rational r );
|
---|
| 57 | Rational ?/?( Rational l, Rational r );
|
---|
[630a82a] | 58 |
|
---|
| 59 | // conversion
|
---|
[53ba273] | 60 | double widen( Rational r );
|
---|
| 61 | Rational narrow( double f, long int md );
|
---|
[630a82a] | 62 |
|
---|
| 63 | // I/O
|
---|
[3d9b5da] | 64 | forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
|
---|
| 65 | forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
|
---|
[53ba273] | 66 |
|
---|
[17e5e2b] | 67 | #endif // RATIONAL_H
|
---|
| 68 |
|
---|
| 69 | #else
|
---|
| 70 | #include_next <rational>
|
---|
| 71 | #endif //__CFORALL__
|
---|
| 72 |
|
---|
[53ba273] | 73 | // Local Variables: //
|
---|
| 74 | // mode: c //
|
---|
| 75 | // tab-width: 4 //
|
---|
| 76 | // End: //
|
---|