source: src/examples/rational.c @ 98ec3a0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 98ec3a0 was d1ab5331, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add constructors to rational type

  • Property mode set to 100644
File size: 2.1 KB
Line 
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//
8// rational.c -- test rational number package
9//
10// Author           : Peter A. Buhr
11// Created On       : Mon Mar 28 08:43:12 2016
12// Last Modified By : Peter A. Buhr
13// Last Modified On : Wed May  4 14:19:36 2016
14// Update Count     : 24
15//
16
17#include <limits>
18#include <rational>
19#include <fstream>
20
21int main() {
22        sout | "constructor" | endl;
23        Rational a = { 3 }, b = { 4 }, c;
24        sout | a | b | c | endl;
25        a = (Rational){ 4, 8 };
26        b = (Rational){ 5, 7 };
27        sout | a | b | endl;
28        a = (Rational){ -2, -3 };
29        b = (Rational){ 3, -2 };
30        sout | a | b | endl;
31        a = (Rational){ -2, 3 };
32        b = (Rational){ 3, 2 };
33        sout | a | b | endl;
34
35        sout | "logical" | endl;
36        a = (Rational){ -2 };
37        b = (Rational){ -3, 2 };
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;
54        a = (Rational){ 3, 4 };
55        sout | widen( a ) | endl;
56        a = (Rational){ 1, 7 };
57        sout | widen( a ) | endl;
58        a = (Rational){ 355, 113 };
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
64        Rational x = { 1, 2 }, y = { 2 };
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
70        Rational z = { 0, 5 };
71        sout | z | endl;
72
73        sout | x | numerator( x, 0 ) | x | endl;
74
75        x = (Rational){ 1, MAX } + (Rational){ 1, MAX };
76        sout | x | endl;
77        x = (Rational){ 3, MAX } + (Rational){ 2, MAX };
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: //
Note: See TracBrowser for help on using the repository browser.