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