source: tests/rational.cfa @ d344a63

Last change on this file since d344a63 was 541dbc09, checked in by Peter A. Buhr <pabuhr@…>, 19 months ago

make type names arithmetic, rational, rat_int lower-case

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[2afec66]1//
[53ba273]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.
[2afec66]6//
[dc8511c]7// rational.cfa -- test rational number package
[2afec66]8//
[53ba273]9// Author           : Peter A. Buhr
10// Created On       : Mon Mar 28 08:43:12 2016
11// Last Modified By : Peter A. Buhr
[541dbc09]12// Last Modified On : Mon Jun  5 22:58:09 2023
13// Update Count     : 108
[2afec66]14//
[53ba273]15
[73abe95]16#include <rational.hfa>
17#include <limits.hfa>
18#include <stdlib.hfa>
19#include <fstream.hfa>
[53ba273]20
[541dbc09]21typedef rational(int) rat_int;
[f00b2c2c]22double convert( int i ) { return (double)i; }                   // used by narrow/widen
[6c6455f]23int convert( double d ) { return (int)d; }
[561f730]24
[53ba273]25int main() {
[200fcb3]26        sout | "constructor";
[541dbc09]27        rat_int a = { 3 }, b = { 4 }, c, d = 0, e = 1;
[d30804a]28        sout | "a : " | a | "b : " | b | "c : " | c | "d : " | d | "e : " | e;
[561f730]29
[541dbc09]30        a = (rat_int){ 4, 8 };
31        b = (rat_int){ 5, 7 };
[d30804a]32        sout | "a : " | a | "b : " | b;
[541dbc09]33        a = (rat_int){ -2, -3 };
34        b = (rat_int){ 3, -2 };
[d30804a]35        sout | "a : " | a | "b : " | b;
[541dbc09]36        a = (rat_int){ -2, 3 };
37        b = (rat_int){ 3, 2 };
[d30804a]38        sout | "a : " | a | "b : " | b;
39        sout | nl;
[53ba273]40
[d30804a]41        sout | "comparison";
[541dbc09]42        a = (rat_int){ -2 };
43        b = (rat_int){ -3, 2 };
[d30804a]44        sout | "a : " | a | "b : " | b;
[541dbc09]45        sout | "a == 0 : " | a == (rational(int)){0}; // FIX ME
46        sout | "a == 1 : " | a == (rational(int)){1}; // FIX ME
[d30804a]47        sout | "a != 0 : " | a != 0;
48        sout | "! a : " | ! a;
49        sout | "a != b : " | a != b;
50        sout | "a <  b : " | a <  b;
51        sout | "a <=  b : " | a <= b;
52        sout | "a >  b : " | a >  b;
53        sout | "a >=  b : " | a >= b;
54        sout | nl;
[53ba273]55
[200fcb3]56        sout | "arithmetic";
[d30804a]57        sout | "a : " | a | "b : " | b;
58        sout | "a + b : " | a + b;
59        sout | "a += b : " | (a += b);
60        sout | "++a : " | ++a;
61        sout | "a++ : " | a++;
62        sout | "a : " | a;
63        sout | "a - b : " | a - b;
64        sout | "a -= b : " | (a -= b);
65        sout | "--a : " | --a;
66        sout | "a-- : " | a--;
67        sout | "a : " | a;
68        sout | "a * b : " | a * b;
69        sout | "a / b : " | a / b;
70        sout | "a \\ 2 : " | a \ 2u | "b \\ 2 : " | b \ 2u;
71        sout | "a \\ -2 : " | a \ -2 | "b \\ -2 : " | b \ -2;
72        sout | nl;
[53ba273]73
[200fcb3]74        sout | "conversion";
[541dbc09]75        a = (rat_int){ 3, 4 };
[200fcb3]76        sout | widen( a );
[541dbc09]77        a = (rat_int){ 1, 7 };
[200fcb3]78        sout | widen( a );
[541dbc09]79        a = (rat_int){ 355, 113 };
[200fcb3]80        sout | widen( a );
81        sout | narrow( 0.75, 4 );
82        sout | narrow( 0.14285714285714, 16 );
83        sout | narrow( 3.14159265358979, 256 );
[d30804a]84        sout | nl;
[53ba273]85
[d30804a]86        // sout | "decompose";
87        // int n, d;
88        // [n, d] = a;
89        // sout | a | n | d;
[f621a148]90
[200fcb3]91        sout | "more tests";
[541dbc09]92        rat_int x = { 1, 2 }, y = { 2 };
[200fcb3]93        sout | x - y;
94        sout | x > y;
95        sout | x | numerator( x, 2 ) | x;
96        sout | y | denominator( y, -2 ) | y;
[53ba273]97
[541dbc09]98        rat_int z = { 0, 5 };
[200fcb3]99        sout | z;
[53ba273]100
[200fcb3]101        sout | x | numerator( x, 0 ) | x;
[53ba273]102
[541dbc09]103        x = (rat_int){ 1, MAX } + (rat_int){ 1, MAX };
[200fcb3]104        sout | x;
[541dbc09]105        x = (rat_int){ 3, MAX } + (rat_int){ 2, MAX };
[200fcb3]106        sout | x;
[53ba273]107
[7bc4e6b]108        sin | a | b;
[200fcb3]109        sout | a | b;
[53ba273]110} // main
111
112// Local Variables: //
113// tab-width: 4 //
[dc8511c]114// compile-command: "cfa rational.cfa" //
[53ba273]115// End: //
Note: See TracBrowser for help on using the repository browser.