| 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.cfa -- test rational number package
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Peter A. Buhr
 | 
|---|
| 10 | // Created On       : Mon Mar 28 08:43:12 2016
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Sat Feb  8 18:46:23 2020
 | 
|---|
| 13 | // Update Count     : 86
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <rational.hfa>
 | 
|---|
| 17 | #include <limits.hfa>
 | 
|---|
| 18 | #include <stdlib.hfa>
 | 
|---|
| 19 | #include <fstream.hfa>
 | 
|---|
| 20 | 
 | 
|---|
| 21 | typedef Rational(int) RatInt;
 | 
|---|
| 22 | double convert( int i ) { return (double)i; }                   // used by narrow/widen
 | 
|---|
| 23 | int convert( double d ) { return (int)d; }
 | 
|---|
| 24 | 
 | 
|---|
| 25 | int main() {
 | 
|---|
| 26 |         sout | "constructor";
 | 
|---|
| 27 |         RatInt a = { 3 }, b = { 4 }, c, d = 0, e = 1;
 | 
|---|
| 28 |         sout | a | b | c | d | e;
 | 
|---|
| 29 | 
 | 
|---|
| 30 |         a = (RatInt){ 4, 8 };
 | 
|---|
| 31 |         b = (RatInt){ 5, 7 };
 | 
|---|
| 32 |         sout | a | b;
 | 
|---|
| 33 |         a = (RatInt){ -2, -3 };
 | 
|---|
| 34 |         b = (RatInt){ 3, -2 };
 | 
|---|
| 35 |         sout | a | b;
 | 
|---|
| 36 |         a = (RatInt){ -2, 3 };
 | 
|---|
| 37 |         b = (RatInt){ 3, 2 };
 | 
|---|
| 38 |         sout | a | b;
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         sout | "logical";
 | 
|---|
| 41 |         a = (RatInt){ -2 };
 | 
|---|
| 42 |         b = (RatInt){ -3, 2 };
 | 
|---|
| 43 |         sout | a | b;
 | 
|---|
| 44 | //      sout | a == 1; // FIX ME
 | 
|---|
| 45 |         sout | a != b;
 | 
|---|
| 46 |         sout | a <  b;
 | 
|---|
| 47 |         sout | a <= b;
 | 
|---|
| 48 |         sout | a >  b;
 | 
|---|
| 49 |         sout | a >= b;
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         sout | "arithmetic";
 | 
|---|
| 52 |         sout | a | b;
 | 
|---|
| 53 |         sout | a + b;
 | 
|---|
| 54 |         sout | a - b;
 | 
|---|
| 55 |         sout | a * b;
 | 
|---|
| 56 |         sout | a / b;
 | 
|---|
| 57 | //      sout | a \ 2 | b \ 2; // FIX ME
 | 
|---|
| 58 | //      sout | a \ -2 | b \ -2;
 | 
|---|
| 59 | 
 | 
|---|
| 60 |         sout | "conversion";
 | 
|---|
| 61 |         a = (RatInt){ 3, 4 };
 | 
|---|
| 62 |         sout | widen( a );
 | 
|---|
| 63 |         a = (RatInt){ 1, 7 };
 | 
|---|
| 64 |         sout | widen( a );
 | 
|---|
| 65 |         a = (RatInt){ 355, 113 };
 | 
|---|
| 66 |         sout | widen( a );
 | 
|---|
| 67 |         sout | narrow( 0.75, 4 );
 | 
|---|
| 68 |         sout | narrow( 0.14285714285714, 16 );
 | 
|---|
| 69 |         sout | narrow( 3.14159265358979, 256 );
 | 
|---|
| 70 | 
 | 
|---|
| 71 |         sout | "decompose";
 | 
|---|
| 72 |         int n, d;
 | 
|---|
| 73 | //      [n, d] = a;
 | 
|---|
| 74 | //      sout | a | n | d;
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         sout | "more tests";
 | 
|---|
| 77 |         RatInt x = { 1, 2 }, y = { 2 };
 | 
|---|
| 78 |         sout | x - y;
 | 
|---|
| 79 |         sout | x > y;
 | 
|---|
| 80 |         sout | x | numerator( x, 2 ) | x;
 | 
|---|
| 81 |         sout | y | denominator( y, -2 ) | y;
 | 
|---|
| 82 | 
 | 
|---|
| 83 |         RatInt z = { 0, 5 };
 | 
|---|
| 84 |         sout | z;
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         sout | x | numerator( x, 0 ) | x;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         x = (RatInt){ 1, MAX } + (RatInt){ 1, MAX };
 | 
|---|
| 89 |         sout | x;
 | 
|---|
| 90 |         x = (RatInt){ 3, MAX } + (RatInt){ 2, MAX };
 | 
|---|
| 91 |         sout | x;
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         sin | a | b;
 | 
|---|
| 94 |         sout | a | b;
 | 
|---|
| 95 | } // main
 | 
|---|
| 96 | 
 | 
|---|
| 97 | // Local Variables: //
 | 
|---|
| 98 | // tab-width: 4 //
 | 
|---|
| 99 | // compile-command: "cfa rational.cfa" //
 | 
|---|
| 100 | // End: //
 | 
|---|