# source:src/tests/rational.c@a6151ba

Last change on this file since a6151ba was a6151ba, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

• Property mode set to `100644`
File size: 2.0 KB
Line
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.c -- test rational number package
8//
9// Author           : Peter A. Buhr
10// Created On       : Mon Mar 28 08:43:12 2016
13// Update Count     : 25
14//
15
16#include <limits>
17#include <rational>
18#include <fstream>
19
20int main() {
21        sout | "constructor" | endl;
22        Rational a = { 3 }, b = { 4 }, c;
23        sout | a | b | c | endl;
24        a = (Rational){ 4, 8 };
25        b = (Rational){ 5, 7 };
26        sout | a | b | endl;
27        a = (Rational){ -2, -3 };
28        b = (Rational){ 3, -2 };
29        sout | a | b | endl;
30        a = (Rational){ -2, 3 };
31        b = (Rational){ 3, 2 };
32        sout | a | b | endl;
33
34        sout | "logical" | endl;
35        a = (Rational){ -2 };
36        b = (Rational){ -3, 2 };
37        sout | a | b | endl;
38        sout | a == 1 | endl;
39        sout | a != b | endl;
40        sout | a <  b | endl;
41        sout | a <= b | endl;
42        sout | a >  b | endl;
43        sout | a >= b | endl;
44
45        sout | "arithmetic" | endl;
46        sout | a | b | endl;
47        sout | a + b | endl;
48        sout | a - b | endl;
49        sout | a * b | endl;
50        sout | a / b | endl;
51
52        sout | "conversion" | endl;
53        a = (Rational){ 3, 4 };
54        sout | widen( a ) | endl;
55        a = (Rational){ 1, 7 };
56        sout | widen( a ) | endl;
57        a = (Rational){ 355, 113 };
58        sout | widen( a ) | endl;
59        sout | narrow( 0.75, 4 ) | endl;
60        sout | narrow( 0.14285714285714, 16 ) | endl;
61        sout | narrow( 3.14159265358979, 256 ) | endl;
62
63        Rational x = { 1, 2 }, y = { 2 };
64        sout | x - y | endl;
65        sout | x > y | endl;
66        sout | x | numerator( x, 2 ) | x | endl;
67        sout | y | denominator( y, -2 ) | y | endl;
68
69        Rational z = { 0, 5 };
70        sout | z | endl;
71
72        sout | x | numerator( x, 0 ) | x | endl;
73
74        x = (Rational){ 1, MAX } + (Rational){ 1, MAX };
75        sout | x | endl;
76        x = (Rational){ 3, MAX } + (Rational){ 2, MAX };
77        sout | x | endl;
78
79        sin | &a | &b;
80        sout | a | b | endl;
81} // main
82
83// Local Variables: //
84// tab-width: 4 //
85// compile-command: "cfa rational.c" //
86// End: //
Note: See TracBrowser for help on using the repository browser.