| [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 | 
|---|
| [09687aa] | 14 | // Last Modified On : Wed Dec  6 23:12:53 2017 | 
|---|
|  | 15 | // Update Count     : 97 | 
|---|
| [bb82c03] | 16 | // | 
|---|
| [f621a148] | 17 |  | 
|---|
| [53a6c2a] | 18 | #pragma once | 
|---|
| [53ba273] | 19 |  | 
|---|
| [3d9b5da] | 20 | #include "iostream" | 
|---|
| [53ba273] | 21 |  | 
|---|
| [561f730] | 22 | trait scalar( otype T ) { | 
|---|
|  | 23 | }; | 
|---|
|  | 24 |  | 
|---|
|  | 25 | trait arithmetic( otype T | scalar( T ) ) { | 
|---|
|  | 26 | int !?( T ); | 
|---|
|  | 27 | int ?==?( T, T ); | 
|---|
|  | 28 | int ?!=?( T, T ); | 
|---|
|  | 29 | int ?<?( T, T ); | 
|---|
|  | 30 | int ?<=?( T, T ); | 
|---|
|  | 31 | int ?>?( T, T ); | 
|---|
|  | 32 | int ?>=?( T, T ); | 
|---|
| [a493682] | 33 | void ?{}( T &, zero_t ); | 
|---|
|  | 34 | void ?{}( T &, one_t ); | 
|---|
| [561f730] | 35 | T +?( T ); | 
|---|
|  | 36 | T -?( T ); | 
|---|
|  | 37 | T ?+?( T, T ); | 
|---|
|  | 38 | T ?-?( T, T ); | 
|---|
|  | 39 | T ?*?( T, T ); | 
|---|
|  | 40 | T ?/?( T, T ); | 
|---|
|  | 41 | T ?%?( T, T ); | 
|---|
| [53a8e68] | 42 | T ?/=?( T &, T ); | 
|---|
| [561f730] | 43 | T abs( T ); | 
|---|
|  | 44 | }; | 
|---|
|  | 45 |  | 
|---|
| [630a82a] | 46 | // implementation | 
|---|
| [561f730] | 47 |  | 
|---|
| [53a6c2a] | 48 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [53ba273] | 49 | struct Rational { | 
|---|
| [561f730] | 50 | RationalImpl numerator, denominator;                            // invariant: denominator > 0 | 
|---|
| [53ba273] | 51 | }; // Rational | 
|---|
|  | 52 |  | 
|---|
| [630a82a] | 53 | // constructors | 
|---|
| [561f730] | 54 |  | 
|---|
| [53a6c2a] | 55 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [a493682] | 56 | void ?{}( Rational(RationalImpl) & r ); | 
|---|
| [561f730] | 57 |  | 
|---|
| [53a6c2a] | 58 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [a493682] | 59 | void ?{}( Rational(RationalImpl) & r, RationalImpl n ); | 
|---|
| [561f730] | 60 |  | 
|---|
| [53a6c2a] | 61 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [a493682] | 62 | void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ); | 
|---|
| [561f730] | 63 |  | 
|---|
| [53a6c2a] | 64 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [a493682] | 65 | void ?{}( Rational(RationalImpl) & r, zero_t ); | 
|---|
| [561f730] | 66 |  | 
|---|
| [53a6c2a] | 67 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [a493682] | 68 | void ?{}( Rational(RationalImpl) & r, one_t ); | 
|---|
| [630a82a] | 69 |  | 
|---|
| [53a6c2a] | 70 | // numerator/denominator getter | 
|---|
| [561f730] | 71 |  | 
|---|
| [53a6c2a] | 72 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 73 | RationalImpl numerator( Rational(RationalImpl) r ); | 
|---|
|  | 74 |  | 
|---|
| [53a6c2a] | 75 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 76 | RationalImpl denominator( Rational(RationalImpl) r ); | 
|---|
| [53a6c2a] | 77 |  | 
|---|
|  | 78 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [a493682] | 79 | [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ); | 
|---|
| [561f730] | 80 |  | 
|---|
| [53a6c2a] | 81 | // numerator/denominator setter | 
|---|
| [561f730] | 82 |  | 
|---|
| [53a6c2a] | 83 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 84 | RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ); | 
|---|
|  | 85 |  | 
|---|
| [53a6c2a] | 86 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 87 | RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ); | 
|---|
| [630a82a] | 88 |  | 
|---|
|  | 89 | // comparison | 
|---|
| [561f730] | 90 |  | 
|---|
| [53a6c2a] | 91 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 92 | int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 93 |  | 
|---|
| [53a6c2a] | 94 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 95 | int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 96 |  | 
|---|
| [53a6c2a] | 97 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 98 | int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 99 |  | 
|---|
| [53a6c2a] | 100 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 101 | int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 102 |  | 
|---|
| [53a6c2a] | 103 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 104 | int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 105 |  | 
|---|
| [53a6c2a] | 106 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 107 | int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
| [630a82a] | 108 |  | 
|---|
|  | 109 | // arithmetic | 
|---|
| [561f730] | 110 |  | 
|---|
| [53a6c2a] | 111 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 112 | Rational(RationalImpl) +?( Rational(RationalImpl) r ); | 
|---|
|  | 113 |  | 
|---|
| [53a6c2a] | 114 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 115 | Rational(RationalImpl) -?( Rational(RationalImpl) r ); | 
|---|
|  | 116 |  | 
|---|
| [53a6c2a] | 117 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 118 | Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 119 |  | 
|---|
| [53a6c2a] | 120 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 121 | Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 122 |  | 
|---|
| [53a6c2a] | 123 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 124 | Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
|  | 125 |  | 
|---|
| [53a6c2a] | 126 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [561f730] | 127 | Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ); | 
|---|
| [630a82a] | 128 |  | 
|---|
|  | 129 | // conversion | 
|---|
| [53a6c2a] | 130 | forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } ) | 
|---|
| [6c6455f] | 131 | double widen( Rational(RationalImpl) r ); | 
|---|
| [53a6c2a] | 132 | forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl );  RationalImpl convert( double );} ) | 
|---|
| [6c6455f] | 133 | Rational(RationalImpl) narrow( double f, RationalImpl md ); | 
|---|
| [630a82a] | 134 |  | 
|---|
|  | 135 | // I/O | 
|---|
| [53a6c2a] | 136 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [09687aa] | 137 | forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } ) | 
|---|
|  | 138 | istype & ?|?( istype &, Rational(RationalImpl) & ); | 
|---|
| [561f730] | 139 |  | 
|---|
| [53a6c2a] | 140 | forall( otype RationalImpl | arithmetic( RationalImpl ) ) | 
|---|
| [09687aa] | 141 | forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) | 
|---|
|  | 142 | ostype & ?|?( ostype &, Rational(RationalImpl ) ); | 
|---|
| [53ba273] | 143 |  | 
|---|
|  | 144 | // Local Variables: // | 
|---|
|  | 145 | // mode: c // | 
|---|
|  | 146 | // tab-width: 4 // | 
|---|
|  | 147 | // End: // | 
|---|