source: src/tests/rational.c@ d36c117

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since d36c117 was 561f730, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt converting rational numbers to generic type

  • Property mode set to 100644
File size: 2.4 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
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun May 14 18:10:28 2017
13// Update Count : 57
14//
15
16#include <rational>
17#include <limits>
18#include <stdlib>
19#include <fstream>
20
21// UNNECESSARY, FIX ME
22void ?{}( int * this ) { *this = 0; }
23void ?{}( int * this, zero_t ) { *this = 0; }
24void ?{}( int * this, one_t ) { *this = 1; }
25
26int main() {
27 sout | "constructor" | endl;
28 Rational(int) a = { 3 }, b = { 4 }, c;
29 sout | a | b | c | endl;
30
31 a = (Rational(int)){ 4, 8 };
32 b = (Rational(int)){ 5, 7 };
33 sout | a | b | endl;
34 a = (Rational(int)){ -2, -3 };
35 b = (Rational(int)){ 3, -2 };
36 sout | a | b | endl;
37 a = (Rational(int)){ -2, 3 };
38 b = (Rational(int)){ 3, 2 };
39 sout | a | b | endl;
40
41 sout | "logical" | endl;
42 a = (Rational(int)){ -2 };
43 b = (Rational(int)){ -3, 2 };
44 sout | a | b | endl;
45// sout | a == 1 | endl; // FIX ME
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 | "arithmetic" | endl;
53 sout | a | b | endl;
54 sout | a + b | endl;
55 sout | a - b | endl;
56 sout | a * b | endl;
57 sout | a / b | endl;
58
59// sout | "conversion" | endl;
60// a = (Rational(int)){ 3, 4 };
61// sout | widen( a ) | endl;
62// a = (Rational(int)){ 1, 7 };
63// sout | widen( a ) | endl;
64// a = (Rational(int)){ 355, 113 };
65// sout | widen( a ) | endl;
66// sout | narrow( 0.75, 4 ) | endl;
67// sout | narrow( 0.14285714285714, 16 ) | endl;
68// sout | narrow( 3.14159265358979, 256 ) | endl;
69
70 sout | "decompose" | endl;
71 int n, d;
72// [n, d] = a;
73// sout | a | n | d | endl;
74
75 sout | "more tests" | endl;
76 Rational(int) x = { 1, 2 }, y = { 2 };
77 sout | x - y | endl;
78 sout | x > y | endl;
79 sout | x | numerator( x, 2 ) | x | endl;
80 sout | y | denominator( y, -2 ) | y | endl;
81
82 Rational(int) z = { 0, 5 };
83 sout | z | endl;
84
85 sout | x | numerator( x, 0 ) | x | endl;
86
87 x = (Rational(int)){ 1, MAX } + (Rational(int)){ 1, MAX };
88 sout | x | endl;
89 x = (Rational(int)){ 3, MAX } + (Rational(int)){ 2, MAX };
90 sout | x | endl;
91
92 sin | &a | &b;
93 sout | a | b | endl;
94} // main
95
96// Local Variables: //
97// tab-width: 4 //
98// compile-command: "cfa rational.c" //
99// End: //
Note: See TracBrowser for help on using the repository browser.