| 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 | // 
 | 
|---|
| 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.
 | 
|---|
| 10 | // 
 | 
|---|
| 11 | // Author           : Peter A. Buhr
 | 
|---|
| 12 | // Created On       : Wed Apr  6 17:56:25 2016
 | 
|---|
| 13 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 14 | // Last Modified On : Fri Apr  8 11:38:27 2016
 | 
|---|
| 15 | // Update Count     : 15
 | 
|---|
| 16 | // 
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "iostream"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | // implementation
 | 
|---|
| 21 | struct Rational {
 | 
|---|
| 22 |         long int numerator, denominator;                                        // invariant: denominator > 0
 | 
|---|
| 23 | }; // Rational
 | 
|---|
| 24 | 
 | 
|---|
| 25 | // constants
 | 
|---|
| 26 | extern struct Rational 0;
 | 
|---|
| 27 | extern struct Rational 1;
 | 
|---|
| 28 | 
 | 
|---|
| 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
 | 
|---|
| 35 | long int numerator( Rational r );
 | 
|---|
| 36 | long int numerator( Rational r, long int n );
 | 
|---|
| 37 | long int denominator( Rational r );
 | 
|---|
| 38 | long int denominator( Rational r, long int d );
 | 
|---|
| 39 | 
 | 
|---|
| 40 | // comparison
 | 
|---|
| 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 );
 | 
|---|
| 47 | 
 | 
|---|
| 48 | // arithmetic
 | 
|---|
| 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 );
 | 
|---|
| 54 | 
 | 
|---|
| 55 | // conversion
 | 
|---|
| 56 | double widen( Rational r );
 | 
|---|
| 57 | Rational narrow( double f, long int md );
 | 
|---|
| 58 | 
 | 
|---|
| 59 | // I/O
 | 
|---|
| 60 | forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
 | 
|---|
| 61 | forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
 | 
|---|
| 62 | 
 | 
|---|
| 63 | // Local Variables: //
 | 
|---|
| 64 | // mode: c //
 | 
|---|
| 65 | // tab-width: 4 //
 | 
|---|
| 66 | // End: //
 | 
|---|