// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // rational.cfa -- test rational number package // // Author : Peter A. Buhr // Created On : Mon Mar 28 08:43:12 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Wed Mar 27 07:37:17 2019 // Update Count : 80 // #include #include #include #include double convert( int i ) { return (double)i; } int convert( double d ) { return (int)d; } int main() { sout | "constructor"; Rational(int) a = { 3 }, b = { 4 }, c; sout | a | b | c; a = (Rational(int)){ 4, 8 }; b = (Rational(int)){ 5, 7 }; sout | a | b; a = (Rational(int)){ -2, -3 }; b = (Rational(int)){ 3, -2 }; sout | a | b; a = (Rational(int)){ -2, 3 }; b = (Rational(int)){ 3, 2 }; sout | a | b; sout | "logical"; a = (Rational(int)){ -2 }; b = (Rational(int)){ -3, 2 }; sout | a | b; // sout | a == 1; // FIX ME sout | a != b; sout | a < b; sout | a <= b; sout | a > b; sout | a >= b; sout | "arithmetic"; sout | a | b; sout | a + b; sout | a - b; sout | a * b; sout | a / b; // sout | a \ 2 | b \ 2; // FIX ME // sout | a \ -2 | b \ -2; sout | "conversion"; a = (Rational(int)){ 3, 4 }; sout | widen( a ); a = (Rational(int)){ 1, 7 }; sout | widen( a ); a = (Rational(int)){ 355, 113 }; sout | widen( a ); sout | narrow( 0.75, 4 ); sout | narrow( 0.14285714285714, 16 ); sout | narrow( 3.14159265358979, 256 ); sout | "decompose"; int n, d; // [n, d] = a; // sout | a | n | d; sout | "more tests"; Rational(int) x = { 1, 2 }, y = { 2 }; sout | x - y; sout | x > y; sout | x | numerator( x, 2 ) | x; sout | y | denominator( y, -2 ) | y; Rational(int) z = { 0, 5 }; sout | z; sout | x | numerator( x, 0 ) | x; x = (Rational(int)){ 1, MAX } + (Rational(int)){ 1, MAX }; sout | x; x = (Rational(int)){ 3, MAX } + (Rational(int)){ 2, MAX }; sout | x; sin | a | b; sout | a | b; } // main // Local Variables: // // tab-width: 4 // // compile-command: "cfa rational.cfa" // // End: //