source: tests/rational.cfa@ 3f3bfe5a

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 3f3bfe5a was 0087e0e, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add rational exponentiation, code clean up

  • Property mode set to 100644
File size: 2.1 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
[0087e0e]12// Last Modified On : Wed Mar 27 07:37:17 2019
13// Update Count : 80
[2afec66]14//
[53ba273]15
[73abe95]16#include <rational.hfa>
17#include <limits.hfa>
18#include <stdlib.hfa>
19#include <fstream.hfa>
[53ba273]20
[6c6455f]21double convert( int i ) { return (double)i; }
22int convert( double d ) { return (int)d; }
[561f730]23
[53ba273]24int main() {
[200fcb3]25 sout | "constructor";
[561f730]26 Rational(int) a = { 3 }, b = { 4 }, c;
[200fcb3]27 sout | a | b | c;
[561f730]28
29 a = (Rational(int)){ 4, 8 };
30 b = (Rational(int)){ 5, 7 };
[200fcb3]31 sout | a | b;
[561f730]32 a = (Rational(int)){ -2, -3 };
33 b = (Rational(int)){ 3, -2 };
[200fcb3]34 sout | a | b;
[561f730]35 a = (Rational(int)){ -2, 3 };
36 b = (Rational(int)){ 3, 2 };
[200fcb3]37 sout | a | b;
[53ba273]38
[200fcb3]39 sout | "logical";
[561f730]40 a = (Rational(int)){ -2 };
41 b = (Rational(int)){ -3, 2 };
[200fcb3]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;
[53ba273]49
[200fcb3]50 sout | "arithmetic";
51 sout | a | b;
52 sout | a + b;
53 sout | a - b;
54 sout | a * b;
55 sout | a / b;
[0087e0e]56// sout | a \ 2 | b \ 2; // FIX ME
57// sout | a \ -2 | b \ -2;
[53ba273]58
[200fcb3]59 sout | "conversion";
[6c6455f]60 a = (Rational(int)){ 3, 4 };
[200fcb3]61 sout | widen( a );
[6c6455f]62 a = (Rational(int)){ 1, 7 };
[200fcb3]63 sout | widen( a );
[6c6455f]64 a = (Rational(int)){ 355, 113 };
[200fcb3]65 sout | widen( a );
66 sout | narrow( 0.75, 4 );
67 sout | narrow( 0.14285714285714, 16 );
68 sout | narrow( 3.14159265358979, 256 );
[53ba273]69
[200fcb3]70 sout | "decompose";
[561f730]71 int n, d;
[39c5ea3]72// [n, d] = a;
[200fcb3]73// sout | a | n | d;
[f621a148]74
[200fcb3]75 sout | "more tests";
[561f730]76 Rational(int) x = { 1, 2 }, y = { 2 };
[200fcb3]77 sout | x - y;
78 sout | x > y;
79 sout | x | numerator( x, 2 ) | x;
80 sout | y | denominator( y, -2 ) | y;
[53ba273]81
[561f730]82 Rational(int) z = { 0, 5 };
[200fcb3]83 sout | z;
[53ba273]84
[200fcb3]85 sout | x | numerator( x, 0 ) | x;
[53ba273]86
[561f730]87 x = (Rational(int)){ 1, MAX } + (Rational(int)){ 1, MAX };
[200fcb3]88 sout | x;
[561f730]89 x = (Rational(int)){ 3, MAX } + (Rational(int)){ 2, MAX };
[200fcb3]90 sout | x;
[53ba273]91
[7bc4e6b]92 sin | a | b;
[200fcb3]93 sout | a | b;
[53ba273]94} // main
95
96// Local Variables: //
97// tab-width: 4 //
[dc8511c]98// compile-command: "cfa rational.cfa" //
[53ba273]99// End: //
Note: See TracBrowser for help on using the repository browser.