// // 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 -- Rational numbers are numbers written as a ratio, i.e., as a fraction, where the numerator (top number) // and the denominator (bottom number) are whole numbers. When creating and computing with rational numbers, results // are constantly reduced to keep the numerator and denominator as small as possible. // // Author : Peter A. Buhr // Created On : Wed Apr 6 17:56:25 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Fri Apr 8 11:38:27 2016 // Update Count : 15 // #include "iostream" // implementation struct Rational { long int numerator, denominator; // invariant: denominator > 0 }; // Rational // constants extern struct Rational 0; extern struct Rational 1; // constructors Rational rational(); Rational rational( long int n ); Rational rational( long int n, long int d ); // getter/setter for numerator/denominator long int numerator( Rational r ); long int numerator( Rational r, long int n ); long int denominator( Rational r ); long int denominator( Rational r, long int d ); // comparison int ?==?( Rational l, Rational r ); int ?!=?( Rational l, Rational r ); int ??( Rational l, Rational r ); int ?>=?( Rational l, Rational r ); // arithmetic Rational -?( Rational r ); Rational ?+?( Rational l, Rational r ); Rational ?-?( Rational l, Rational r ); Rational ?*?( Rational l, Rational r ); Rational ?/?( Rational l, Rational r ); // conversion double widen( Rational r ); Rational narrow( double f, long int md ); // I/O forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational ); // Local Variables: // // mode: c // // tab-width: 4 // // End: //