# Changeset 541dbc09

Ignore:
Timestamp:
Jun 6, 2023, 8:44:14 AM (13 months ago)
Branches:
ast-experimental, master
Children:
77afbb4
Parents:
6a93e4d
Message:

make type names arithmetic, rational, rat_int lower-case

Files:
3 edited

Unmodified
Removed
• ## libcfa/src/rational.cfa

 r6a93e4d // Created On       : Wed Apr  6 17:54:28 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Thu Aug 25 18:09:58 2022 // Update Count     : 194 // Last Modified On : Mon Jun  5 22:49:06 2023 // Update Count     : 196 // #pragma GCC visibility push(default) forall( T | Arithmetic( T ) ) { forall( T | arithmetic( T ) ) { // helper routines abort | "Invalid rational number construction: denominator cannot be equal to 0."; } // exit if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator if ( d < (T){0} ) { d = -d; n = -n; }                   // move sign to numerator return gcd( abs( n ), d );                                              // simplify } // Rationalnumber::simplify } // simplify // constructors void ?{}( Rational(T) & r, zero_t ) { void ?{}( rational(T) & r, zero_t ) { r{ (T){0}, (T){1} }; } // rational void ?{}( Rational(T) & r, one_t ) { void ?{}( rational(T) & r, one_t ) { r{ (T){1}, (T){1} }; } // rational void ?{}( Rational(T) & r ) { void ?{}( rational(T) & r ) { r{ (T){0}, (T){1} }; } // rational void ?{}( Rational(T) & r, T n ) { 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 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 ) { T numerator( rational(T) r ) { return r.numerator; } // numerator T denominator( Rational(T) r ) { T denominator( rational(T) r ) { return r.denominator; } // denominator [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ) { [ T, T ] ?=?( & [ T, T ] dest, rational(T) src ) { return dest = src.[ numerator, denominator ]; } // ?=? // setter for numerator/denominator T numerator( Rational(T) r, T n ) { T numerator( rational(T) r, T n ) { T prev = r.numerator; T t = gcd( abs( n ), r.denominator ); // simplify 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 denominator( rational(T) r, T d ) { T prev = r.denominator; T t = simplify( r.numerator, d );       // simplify T t = simplify( r.numerator, d );                               // simplify r.[numerator, denominator] = [r.numerator / t, d / t]; return prev; // comparison int ?==?( Rational(T) l, Rational(T) r ) { int ?==?( rational(T) l, rational(T) r ) { return l.numerator * r.denominator == l.denominator * r.numerator; } // ?==? int ?!=?( Rational(T) l, Rational(T) r ) { 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, zero_t ) { return ! ( l == (rational(T)){ 0 } ); } // ?!=? int ??( Rational(T) l, Rational(T) r ) { int ?>?( rational(T) l, rational(T) r ) { return ! ( l <= r ); } // ?>? int ?>=?( Rational(T) l, Rational(T) 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) 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 ) { rational(T) ?+?( rational(T) l, rational(T) r ) { if ( l.denominator == r.denominator ) {                 // special case return (Rational(T)){ l.numerator + r.numerator, l.denominator }; 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 }; return (rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; } // if } // ?+? Rational(T) ?+=?( Rational(T) & l, Rational(T) r ) { 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 }; rational(T) ?+=?( rational(T) & l, one_t ) { l = l + (rational(T)){ 1 }; return l; } // ?+? Rational(T) ?-?( Rational(T) l, Rational(T) r ) { rational(T) ?-?( rational(T) l, rational(T) r ) { if ( l.denominator == r.denominator ) {                 // special case return (Rational(T)){ l.numerator - r.numerator, l.denominator }; 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 }; return (rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; } // if } // ?-? Rational(T) ?-=?( Rational(T) & l, Rational(T) r ) { 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 }; 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 (rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; } // ?*? Rational(T) ?*=?( Rational(T) & l, Rational(T) r ) { rational(T) ?*=?( rational(T) & l, rational(T) r ) { return l = l * r; } // ?*? Rational(T) ?/?( Rational(T) l, Rational(T) 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 }; return (rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator }; } // ?/? Rational(T) ?/=?( Rational(T) & l, Rational(T) r ) { rational(T) ?/=?( rational(T) & l, rational(T) r ) { return l = l / r; } // ?/? forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) istype & ?|?( istype & is, Rational(T) & r ) { istype & ?|?( istype & is, rational(T) & r ) { is | r.numerator | r.denominator; T t = simplify( r.numerator, r.denominator ); forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { ostype & ?|?( ostype & os, Rational(T) r ) { ostype & ?|?( ostype & os, rational(T) r ) { return os | r.numerator | '/' | r.denominator; } // ?|? void ?|?( ostype & os, Rational(T) r ) { void ?|?( ostype & os, rational(T) r ) { (ostype &)(os | r); ends( os ); } // ?|? } // distribution forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { Rational(T) ?\?( Rational(T) x, long int y ) { 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 }; return (rational(T)){ x.denominator \ -y, x.numerator \ -y }; } else { return (Rational(T)){ x.numerator \ y, x.denominator \ y }; return (rational(T)){ x.numerator \ y, x.denominator \ y }; } // if } // ?\? Rational(T) ?\=?( Rational(T) & x, long int y ) { rational(T) ?\=?( rational(T) & x, long int y ) { return x = x \ y; } // ?\? // conversion forall( T | Arithmetic( T ) | { double convert( T ); } ) double widen( Rational(T) r ) { 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 ) { 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 ( md <= (T){1} ) {                                                           // maximum fractional digits too small? return (rational(T)){ convert( f ), (T){1}};    // truncate fraction } // if if ( f > (double)0x7FFFFFFF ) break;                          // representation failure } // for return (Rational(T)){ m00, m10 }; return (rational(T)){ m00, m10 }; } // narrow
• ## libcfa/src/rational.hfa

 r6a93e4d // Created On       : Wed Apr  6 17:56:25 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Tue Jul 20 17:45:29 2021 // Update Count     : 118 // Last Modified On : Mon Jun  5 22:49:05 2023 // Update Count     : 119 // #include "iostream.hfa" #include "math.trait.hfa"                                                               // Arithmetic #include "math.trait.hfa"                                                               // arithmetic // implementation forall( T | Arithmetic( T ) ) { struct Rational { forall( T | arithmetic( T ) ) { struct rational { T numerator, denominator;                                               // invariant: denominator > 0 }; // Rational }; // rational // 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 ); 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 ] dest, Rational(T) src ); T numerator( rational(T) r ); T denominator( rational(T) r ); [ T, T ] ?=?( & [ T, T ] dest, rational(T) src ); // numerator/denominator setter T numerator( Rational(T) r, T n ); T denominator( Rational(T) r, T d ); 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 ); 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 ); 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 ); // I/O forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) istype & ?|?( istype &, Rational(T) & ); istype & ?|?( istype &, rational(T) & ); forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { ostype & ?|?( ostype &, Rational(T) ); void ?|?( ostype &, Rational(T) ); ostype & ?|?( ostype &, rational(T) ); void ?|?( ostype &, rational(T) ); } // distribution } // distribution forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { Rational(T) ?\?( Rational(T) x, long int y ); Rational(T) ?\=?( Rational(T) & x, long int y ); 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 ); 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: //
• ## tests/rational.cfa

 r6a93e4d // Created On       : Mon Mar 28 08:43:12 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Tue Jul 20 18:13:40 2021 // Update Count     : 107 // Last Modified On : Mon Jun  5 22:58:09 2023 // Update Count     : 108 // #include typedef Rational(int) RatInt; typedef rational(int) rat_int; double convert( int i ) { return (double)i; }                   // used by narrow/widen int convert( double d ) { return (int)d; } int main() { sout | "constructor"; RatInt a = { 3 }, b = { 4 }, c, d = 0, e = 1; rat_int a = { 3 }, b = { 4 }, c, d = 0, e = 1; sout | "a : " | a | "b : " | b | "c : " | c | "d : " | d | "e : " | e; a = (RatInt){ 4, 8 }; b = (RatInt){ 5, 7 }; a = (rat_int){ 4, 8 }; b = (rat_int){ 5, 7 }; sout | "a : " | a | "b : " | b; a = (RatInt){ -2, -3 }; b = (RatInt){ 3, -2 }; a = (rat_int){ -2, -3 }; b = (rat_int){ 3, -2 }; sout | "a : " | a | "b : " | b; a = (RatInt){ -2, 3 }; b = (RatInt){ 3, 2 }; a = (rat_int){ -2, 3 }; b = (rat_int){ 3, 2 }; sout | "a : " | a | "b : " | b; sout | nl; sout | "comparison"; a = (RatInt){ -2 }; b = (RatInt){ -3, 2 }; a = (rat_int){ -2 }; b = (rat_int){ -3, 2 }; sout | "a : " | a | "b : " | b; sout | "a == 0 : " | a == (Rational(int)){0}; // FIX ME sout | "a == 1 : " | a == (Rational(int)){1}; // FIX ME sout | "a == 0 : " | a == (rational(int)){0}; // FIX ME sout | "a == 1 : " | a == (rational(int)){1}; // FIX ME sout | "a != 0 : " | a != 0; sout | "! a : " | ! a; sout | "conversion"; a = (RatInt){ 3, 4 }; a = (rat_int){ 3, 4 }; sout | widen( a ); a = (RatInt){ 1, 7 }; a = (rat_int){ 1, 7 }; sout | widen( a ); a = (RatInt){ 355, 113 }; a = (rat_int){ 355, 113 }; sout | widen( a ); sout | narrow( 0.75, 4 ); sout | "more tests"; RatInt x = { 1, 2 }, y = { 2 }; rat_int x = { 1, 2 }, y = { 2 }; sout | x - y; sout | x > y; sout | y | denominator( y, -2 ) | y; RatInt z = { 0, 5 }; rat_int z = { 0, 5 }; sout | z; sout | x | numerator( x, 0 ) | x; x = (RatInt){ 1, MAX } + (RatInt){ 1, MAX }; x = (rat_int){ 1, MAX } + (rat_int){ 1, MAX }; sout | x; x = (RatInt){ 3, MAX } + (RatInt){ 2, MAX }; x = (rat_int){ 3, MAX } + (rat_int){ 2, MAX }; sout | x;
Note: See TracChangeset for help on using the changeset viewer.