// // 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 : Wed Nov 27 18:11:07 2024 // Update Count : 128 // #pragma once #include "iostream.hfa" #include "math.trait.hfa" // arithmetic // Implementation forall( T ) { struct rational { T numerator, denominator; // invariant: denominator > 0 }; // rational } // Arithmetic, Relational forall( T | arithmetic( T ) ) { // constructors void ?{}( rational(T) & r ); void ?{}( rational(T) & r, zero_t ); void ?{}( rational(T) & r, one_t ); void ?{}( rational(T) & r, T n ); void ?{}( rational(T) & r, T n, T d ); // numerator/denominator getter T numerator( rational(T) r ); T denominator( rational(T) r ); [ T, T ] ?=?( & [ T, T ] dst, rational(T) src ); // numerator/denominator setter T numerator( rational(T) r, T n ); T denominator( rational(T) r, T d ); // comparison int ?==?( rational(T) l, rational(T) r ); int ?!=?( rational(T) l, rational(T) r ); int ?!=?( rational(T) l, zero_t ); // => ! int ??( rational(T) l, rational(T) r ); int ?>=?( rational(T) l, rational(T) r ); // arithmetic rational(T) +?( rational(T) r ); rational(T) -?( rational(T) r ); rational(T) ?+?( rational(T) l, rational(T) r ); rational(T) ?+=?( rational(T) & l, rational(T) r ); rational(T) ?+=?( rational(T) & l, one_t ); // => ++?, ?++ rational(T) ?-?( rational(T) l, rational(T) r ); rational(T) ?-=?( rational(T) & l, rational(T) r ); rational(T) ?-=?( rational(T) & l, one_t ); // => --?, ?-- rational(T) ?*?( rational(T) l, rational(T) r ); rational(T) ?*=?( rational(T) & l, rational(T) r ); rational(T) ?/?( rational(T) l, rational(T) r ); rational(T) ?/=?( rational(T) & l, rational(T) r ); } // distribution // I/O forall( T ) { forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } | arithmetic( T ) ) istype & ?|?( istype &, rational(T) & ); forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { ostype & ?|?( ostype &, rational(T) ); OSTYPE_VOID( rational(T) ); } // distribution } // distribution // Exponentiation forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { rational(T) ?\?( rational(T) x, long int y ); rational(T) ?\=?( rational(T) & x, long int y ); } // distribution // Conversion forall( T | arithmetic( T ) | { double convert( T ); } ) double widen( rational(T) r ); forall( T | arithmetic( T ) | { double convert( T ); T convert( double );} ) rational(T) narrow( double f, T md ); // Local Variables: // // mode: c // // tab-width: 4 // // End: //