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