// // 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 | multiplicative(T) | equality(T)) trait Simple { int ?