[53ba273] | 1 | // -*- Mode: C -*- |
---|
| 2 | // |
---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 4 | // |
---|
| 5 | // The contents of this file are covered under the licence agreement in the |
---|
| 6 | // file "LICENCE" distributed with Cforall. |
---|
| 7 | // |
---|
[630a82a] | 8 | // rational.c -- test rational number package |
---|
[53ba273] | 9 | // |
---|
| 10 | // Author : Peter A. Buhr |
---|
| 11 | // Created On : Mon Mar 28 08:43:12 2016 |
---|
| 12 | // Last Modified By : Peter A. Buhr |
---|
[d1ab5331] | 13 | // Last Modified On : Wed May 4 14:19:36 2016 |
---|
| 14 | // Update Count : 24 |
---|
[53ba273] | 15 | // |
---|
| 16 | |
---|
| 17 | #include <limits> |
---|
| 18 | #include <rational> |
---|
[3d9b5da] | 19 | #include <fstream> |
---|
[53ba273] | 20 | |
---|
| 21 | int main() { |
---|
| 22 | sout | "constructor" | endl; |
---|
[d1ab5331] | 23 | Rational a = { 3 }, b = { 4 }, c; |
---|
[53ba273] | 24 | sout | a | b | c | endl; |
---|
[d1ab5331] | 25 | a = (Rational){ 4, 8 }; |
---|
| 26 | b = (Rational){ 5, 7 }; |
---|
[53ba273] | 27 | sout | a | b | endl; |
---|
[d1ab5331] | 28 | a = (Rational){ -2, -3 }; |
---|
| 29 | b = (Rational){ 3, -2 }; |
---|
[53ba273] | 30 | sout | a | b | endl; |
---|
[d1ab5331] | 31 | a = (Rational){ -2, 3 }; |
---|
| 32 | b = (Rational){ 3, 2 }; |
---|
[53ba273] | 33 | sout | a | b | endl; |
---|
| 34 | |
---|
| 35 | sout | "logical" | endl; |
---|
[d1ab5331] | 36 | a = (Rational){ -2 }; |
---|
| 37 | b = (Rational){ -3, 2 }; |
---|
[53ba273] | 38 | sout | a | b | endl; |
---|
| 39 | sout | a == 1 | endl; |
---|
| 40 | sout | a != b | endl; |
---|
| 41 | sout | a < b | endl; |
---|
| 42 | sout | a <= b | endl; |
---|
| 43 | sout | a > b | endl; |
---|
| 44 | sout | a >= b | endl; |
---|
| 45 | |
---|
| 46 | sout | "arithmetic" | endl; |
---|
| 47 | sout | a | b | endl; |
---|
| 48 | sout | a + b | endl; |
---|
| 49 | sout | a - b | endl; |
---|
| 50 | sout | a * b | endl; |
---|
| 51 | sout | a / b | endl; |
---|
| 52 | |
---|
| 53 | sout | "conversion" | endl; |
---|
[d1ab5331] | 54 | a = (Rational){ 3, 4 }; |
---|
[53ba273] | 55 | sout | widen( a ) | endl; |
---|
[d1ab5331] | 56 | a = (Rational){ 1, 7 }; |
---|
[53ba273] | 57 | sout | widen( a ) | endl; |
---|
[d1ab5331] | 58 | a = (Rational){ 355, 113 }; |
---|
[53ba273] | 59 | sout | widen( a ) | endl; |
---|
| 60 | sout | narrow( 0.75, 4 ) | endl; |
---|
| 61 | sout | narrow( 0.14285714285714, 16 ) | endl; |
---|
| 62 | sout | narrow( 3.14159265358979, 256 ) | endl; |
---|
| 63 | |
---|
[d1ab5331] | 64 | Rational x = { 1, 2 }, y = { 2 }; |
---|
[53ba273] | 65 | sout | x - y | endl; |
---|
| 66 | sout | x > y | endl; |
---|
| 67 | sout | x | numerator( x, 2 ) | x | endl; |
---|
| 68 | sout | y | denominator( y, -2 ) | y | endl; |
---|
| 69 | |
---|
[d1ab5331] | 70 | Rational z = { 0, 5 }; |
---|
[53ba273] | 71 | sout | z | endl; |
---|
| 72 | |
---|
| 73 | sout | x | numerator( x, 0 ) | x | endl; |
---|
| 74 | |
---|
[d1ab5331] | 75 | x = (Rational){ 1, MAX } + (Rational){ 1, MAX }; |
---|
[53ba273] | 76 | sout | x | endl; |
---|
[d1ab5331] | 77 | x = (Rational){ 3, MAX } + (Rational){ 2, MAX }; |
---|
[53ba273] | 78 | sout | x | endl; |
---|
| 79 | |
---|
| 80 | sin | &a | &b; |
---|
| 81 | sout | a | b | endl; |
---|
| 82 | } // main |
---|
| 83 | |
---|
| 84 | // Local Variables: // |
---|
| 85 | // tab-width: 4 // |
---|
| 86 | // compile-command: "cfa rational.c" // |
---|
| 87 | // End: // |
---|