| [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 | 
|---|
| [f621a148] | 14 | // Last Modified On : Mon May  1 08:25:06 2017 | 
|---|
|  | 15 | // Update Count     : 33 | 
|---|
| [bb82c03] | 16 | // | 
|---|
| [f621a148] | 17 |  | 
|---|
| [17e5e2b] | 18 | #ifndef RATIONAL_H | 
|---|
|  | 19 | #define RATIONAL_H | 
|---|
| [53ba273] | 20 |  | 
|---|
| [3d9b5da] | 21 | #include "iostream" | 
|---|
| [53ba273] | 22 |  | 
|---|
| [630a82a] | 23 | // implementation | 
|---|
| [f621a148] | 24 | typedef long int RationalImpl; | 
|---|
| [53ba273] | 25 | struct Rational { | 
|---|
| [f621a148] | 26 | RationalImpl numerator, denominator;                                    // invariant: denominator > 0 | 
|---|
| [53ba273] | 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 ); | 
|---|
| [f621a148] | 35 | void ?{}( Rational * r, RationalImpl n ); | 
|---|
|  | 36 | void ?{}( Rational * r, RationalImpl n, RationalImpl d ); | 
|---|
| [630a82a] | 37 |  | 
|---|
| [f621a148] | 38 | // getter for numerator/denominator | 
|---|
|  | 39 | RationalImpl numerator( Rational r ); | 
|---|
|  | 40 | RationalImpl denominator( Rational r ); | 
|---|
|  | 41 | [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src ); | 
|---|
|  | 42 | // setter for numerator/denominator | 
|---|
|  | 43 | RationalImpl numerator( Rational r, RationalImpl n ); | 
|---|
|  | 44 | RationalImpl denominator( Rational r, RationalImpl d ); | 
|---|
| [630a82a] | 45 |  | 
|---|
|  | 46 | // comparison | 
|---|
| [53ba273] | 47 | int ?==?( Rational l, Rational r ); | 
|---|
|  | 48 | int ?!=?( Rational l, Rational r ); | 
|---|
|  | 49 | int ?<?( Rational l, Rational r ); | 
|---|
|  | 50 | int ?<=?( Rational l, Rational r ); | 
|---|
|  | 51 | int ?>?( Rational l, Rational r ); | 
|---|
|  | 52 | int ?>=?( Rational l, Rational r ); | 
|---|
| [630a82a] | 53 |  | 
|---|
|  | 54 | // arithmetic | 
|---|
| [53ba273] | 55 | Rational -?( Rational r ); | 
|---|
|  | 56 | Rational ?+?( Rational l, Rational r ); | 
|---|
|  | 57 | Rational ?-?( Rational l, Rational r ); | 
|---|
|  | 58 | Rational ?*?( Rational l, Rational r ); | 
|---|
|  | 59 | Rational ?/?( Rational l, Rational r ); | 
|---|
| [630a82a] | 60 |  | 
|---|
|  | 61 | // conversion | 
|---|
| [53ba273] | 62 | double widen( Rational r ); | 
|---|
| [f621a148] | 63 | Rational narrow( double f, RationalImpl md ); | 
|---|
| [630a82a] | 64 |  | 
|---|
|  | 65 | // I/O | 
|---|
| [3d9b5da] | 66 | forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); | 
|---|
|  | 67 | forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational ); | 
|---|
| [53ba273] | 68 |  | 
|---|
| [17e5e2b] | 69 | #endif // RATIONAL_H | 
|---|
|  | 70 |  | 
|---|
| [53ba273] | 71 | // Local Variables: // | 
|---|
|  | 72 | // mode: c // | 
|---|
|  | 73 | // tab-width: 4 // | 
|---|
|  | 74 | // End: // | 
|---|