// // 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.c -- // // Author : Peter A. Buhr // Created On : Wed Apr 6 17:54:28 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Mon Nov 11 22:37:12 2024 // Update Count : 206 // #include "rational.hfa" #include "fstream.hfa" #include "stdlib.hfa" #pragma GCC visibility push(default) forall( T | arithmetic( T ) ) { // helper routines // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce // rationals. alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm static T gcd( T a, T b ) { for () { // Euclid's algorithm T r = a % b; if ( r == (T){0} ) break; a = b; b = r; } // for return b; } // gcd static T simplify( T & n, T & d ) { if ( d == (T){0} ) { abort | "Invalid rational number construction: denominator cannot be equal to 0."; } // exit if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator return gcd( abs( n ), d ); // simplify } // simplify // constructors void ?{}( rational(T) & r, zero_t ) { r{ (T){0}, (T){1} }; } // rational void ?{}( rational(T) & r, one_t ) { r{ (T){1}, (T){1} }; } // rational void ?{}( rational(T) & r ) { r{ (T){0}, (T){1} }; } // rational void ?{}( rational(T) & r, T n ) { r{ n, (T){1} }; } // rational void ?{}( rational(T) & r, T n, T d ) { T t = simplify( n, d ); // simplify r.[numerator, denominator] = [n / t, d / t]; } // rational // getter for numerator/denominator T numerator( rational(T) r ) { return r.numerator; } // numerator T denominator( rational(T) r ) { return r.denominator; } // denominator [ T, T ] ?=?( & [ T, T ] dst, rational(T) src ) { return dst = src.[ numerator, denominator ]; } // ?=? // setter for numerator/denominator T numerator( rational(T) r, T n ) { T prev = r.numerator; T t = gcd( abs( n ), r.denominator ); // simplify r.[numerator, denominator] = [n / t, r.denominator / t]; return prev; } // numerator T denominator( rational(T) r, T d ) { T prev = r.denominator; T t = simplify( r.numerator, d ); // simplify r.[numerator, denominator] = [r.numerator / t, d / t]; return prev; } // denominator // comparison int ?==?( rational(T) l, rational(T) r ) { return l.numerator * r.denominator == l.denominator * r.numerator; } // ?==? int ?!=?( rational(T) l, rational(T) r ) { return ! ( l == r ); } // ?!=? int ?!=?( rational(T) l, zero_t ) { return ! ( l == (rational(T)){ 0 } ); } // ?!=? int ??( rational(T) l, rational(T) r ) { return ! ( l <= r ); } // ?>? int ?>=?( rational(T) l, rational(T) r ) { return ! ( l < r ); } // ?>=? // arithmetic rational(T) +?( rational(T) r ) { return (rational(T)){ r.numerator, r.denominator }; } // +? rational(T) -?( rational(T) r ) { return (rational(T)){ -r.numerator, r.denominator }; } // -? rational(T) ?+?( rational(T) l, rational(T) r ) { if ( l.denominator == r.denominator ) { // special case return (rational(T)){ l.numerator + r.numerator, l.denominator }; } else { return (rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; } // if } // ?+? rational(T) ?+=?( rational(T) & l, rational(T) r ) { l = l + r; return l; } // ?+? rational(T) ?+=?( rational(T) & l, one_t ) { l = l + (rational(T)){ 1 }; return l; } // ?+? rational(T) ?-?( rational(T) l, rational(T) r ) { if ( l.denominator == r.denominator ) { // special case return (rational(T)){ l.numerator - r.numerator, l.denominator }; } else { return (rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; } // if } // ?-? rational(T) ?-=?( rational(T) & l, rational(T) r ) { l = l - r; return l; } // ?-? rational(T) ?-=?( rational(T) & l, one_t ) { l = l - (rational(T)){ 1 }; return l; } // ?-? rational(T) ?*?( rational(T) l, rational(T) r ) { return (rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; } // ?*? rational(T) ?*=?( rational(T) & l, rational(T) r ) { return l = l * r; } // ?*? rational(T) ?/?( rational(T) l, rational(T) r ) { if ( r.numerator < (T){0} ) { r.[numerator, denominator] = [-r.numerator, -r.denominator]; } // if return (rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator }; } // ?/? rational(T) ?/=?( rational(T) & l, rational(T) r ) { return l = l / r; } // ?/? // I/O forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) istype & ?|?( istype & is, rational(T) & r ) { is | r.numerator | r.denominator; T t = simplify( r.numerator, r.denominator ); r.numerator /= t; r.denominator /= t; return is; } // ?|? forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { ostype & ?|?( ostype & os, rational(T) r ) { return os | r.numerator | '/' | r.denominator; } // ?|? OSTYPE_VOID_IMPL( os, rational(T) ) } // distribution } // distribution forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { rational(T) ?\?( rational(T) x, long int y ) { if ( y < 0 ) { return (rational(T)){ x.denominator \ -y, x.numerator \ -y }; } else { return (rational(T)){ x.numerator \ y, x.denominator \ y }; } // if } // ?\? rational(T) ?\=?( rational(T) & x, long int y ) { return x = x \ y; } // ?\? } // distribution // conversion forall( T | arithmetic( T ) | { double convert( T ); } ) double widen( rational(T) r ) { return convert( r.numerator ) / convert( r.denominator ); } // widen forall( T | arithmetic( T ) | { double convert( T ); T convert( double ); } ) rational(T) narrow( double f, T md ) { // http://www.ics.uci.edu/~eppstein/numth/frap.c if ( md <= (T){1} ) { // maximum fractional digits too small? return (rational(T)){ convert( f ), (T){1}}; // truncate fraction } // if // continued fraction coefficients T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; T ai, t; // find terms until denom gets too big for () { ai = convert( f ); if ( ! (m10 * ai + m11 <= md) ) break; t = m00 * ai + m01; m01 = m00; m00 = t; t = m10 * ai + m11; m11 = m10; m10 = t; double temp = convert( ai ); if ( f == temp ) break; // prevent division by zero f = 1 / (f - temp); if ( f > (double)0x7FFFFFFF ) break; // representation failure } // for return (rational(T)){ m00, m10 }; } // narrow // Local Variables: // // tab-width: 4 // // End: //