# source:tests/rational.cfa@0c81320

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

add one_t constructor to prelude, remove one_t constructor from test programs

• Property mode set to `100644`
File size: 2.1 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.cfa -- test rational number package
8//
9// Author           : Peter A. Buhr
10// Created On       : Mon Mar 28 08:43:12 2016
13// Update Count     : 73
14//
15
16#include <rational.hfa>
17#include <limits.hfa>
18#include <stdlib.hfa>
19#include <fstream.hfa>
20
21double convert( int i ) { return (double)i; }
22int convert( double d ) { return (int)d; }
23
24int 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
57        sout | "conversion";
58        a = (Rational(int)){ 3, 4 };
59        sout | widen( a );
60        a = (Rational(int)){ 1, 7 };
61        sout | widen( a );
62        a = (Rational(int)){ 355, 113 };
63        sout | widen( a );
64        sout | narrow( 0.75, 4 );
65        sout | narrow( 0.14285714285714, 16 );
66        sout | narrow( 3.14159265358979, 256 );
67
68        sout | "decompose";
69        int n, d;
70//      [n, d] = a;
71//      sout | a | n | d;
72
73        sout | "more tests";
74        Rational(int) x = { 1, 2 }, y = { 2 };
75        sout | x - y;
76        sout | x > y;
77        sout | x | numerator( x, 2 ) | x;
78        sout | y | denominator( y, -2 ) | y;
79
80        Rational(int) z = { 0, 5 };
81        sout | z;
82
83        sout | x | numerator( x, 0 ) | x;
84
85        x = (Rational(int)){ 1, MAX } + (Rational(int)){ 1, MAX };
86        sout | x;
87        x = (Rational(int)){ 3, MAX } + (Rational(int)){ 2, MAX };
88        sout | x;
89
90        sin | a | b;
91        sout | a | b;
92} // main
93
94// Local Variables: //
95// tab-width: 4 //
96// compile-command: "cfa rational.cfa" //
97// End: //
Note: See TracBrowser for help on using the repository browser.