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