Changeset 5dc4c7e for libcfa/src/rational.hfa
 Timestamp:
 Jul 20, 2021, 6:30:29 PM (3 years ago)
 Branches:
 ADT, astexperimental, enum, forallpointerdecay, jacob/cs343translation, master, newastuniqueexpr, pthreademulation, qualifiedEnum
 Children:
 d30804a
 Parents:
 8477fc4
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

libcfa/src/rational.hfa
r8477fc4 r5dc4c7e 12 12 // Created On : Wed Apr 6 17:56:25 2016 13 13 // Last Modified By : Peter A. Buhr 14 // Last Modified On : Tue Mar 26 23:16:10 201915 // Update Count : 1 0914 // Last Modified On : Tue Jul 20 17:45:29 2021 15 // Update Count : 118 16 16 // 17 17 … … 19 19 20 20 #include "iostream.hfa" 21 22 trait scalar( T ) { 23 }; 24 25 trait arithmetic( T  scalar( T ) ) { 26 int !?( T ); 27 int ?==?( T, T ); 28 int ?!=?( T, T ); 29 int ?<?( T, T ); 30 int ?<=?( T, T ); 31 int ?>?( T, T ); 32 int ?>=?( T, T ); 33 void ?{}( T &, zero_t ); 34 void ?{}( T &, one_t ); 35 T +?( T ); 36 T ?( T ); 37 T ?+?( T, T ); 38 T ??( T, T ); 39 T ?*?( T, T ); 40 T ?/?( T, T ); 41 T ?%?( T, T ); 42 T ?/=?( T &, T ); 43 T abs( T ); 44 }; 21 #include "math.trait.hfa" // Arithmetic 45 22 46 23 // implementation 47 24 48 forall( RationalImpl  arithmetic( RationalImpl) ) {25 forall( T  Arithmetic( T ) ) { 49 26 struct Rational { 50 RationalImpl numerator, denominator;// invariant: denominator > 027 T numerator, denominator; // invariant: denominator > 0 51 28 }; // Rational 52 29 53 30 // constructors 54 31 55 void ?{}( Rational( RationalImpl) & r );56 void ?{}( Rational( RationalImpl) & r, RationalImpl n);57 void ?{}( Rational( RationalImpl) & r, RationalImpl n, RationalImpl d);58 void ?{}( Rational( RationalImpl) & r, zero_t);59 void ?{}( Rational( RationalImpl) & r, one_t);32 void ?{}( Rational(T) & r ); 33 void ?{}( Rational(T) & r, zero_t ); 34 void ?{}( Rational(T) & r, one_t ); 35 void ?{}( Rational(T) & r, T n ); 36 void ?{}( Rational(T) & r, T n, T d ); 60 37 61 38 // numerator/denominator getter 62 39 63 RationalImpl numerator( Rational(RationalImpl) r );64 RationalImpl denominator( Rational(RationalImpl) r );65 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src );40 T numerator( Rational(T) r ); 41 T denominator( Rational(T) r ); 42 [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ); 66 43 67 44 // numerator/denominator setter 68 45 69 RationalImpl numerator( Rational(RationalImpl) r, RationalImpln );70 RationalImpl denominator( Rational(RationalImpl) r, RationalImpld );46 T numerator( Rational(T) r, T n ); 47 T denominator( Rational(T) r, T d ); 71 48 72 49 // comparison 73 50 74 int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 75 int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 76 int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 77 int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 78 int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 79 int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 51 int ?==?( Rational(T) l, Rational(T) r ); 52 int ?!=?( Rational(T) l, Rational(T) r ); 53 int ?!=?( Rational(T) l, zero_t ); // => ! 54 int ?<?( Rational(T) l, Rational(T) r ); 55 int ?<=?( Rational(T) l, Rational(T) r ); 56 int ?>?( Rational(T) l, Rational(T) r ); 57 int ?>=?( Rational(T) l, Rational(T) r ); 80 58 81 59 // arithmetic 82 60 83 Rational(RationalImpl) +?( Rational(RationalImpl) r ); 84 Rational(RationalImpl) ?( Rational(RationalImpl) r ); 85 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 86 Rational(RationalImpl) ??( Rational(RationalImpl) l, Rational(RationalImpl) r ); 87 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 88 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ); 61 Rational(T) +?( Rational(T) r ); 62 Rational(T) ?( Rational(T) r ); 63 Rational(T) ?+?( Rational(T) l, Rational(T) r ); 64 Rational(T) ?+=?( Rational(T) & l, Rational(T) r ); 65 Rational(T) ?+=?( Rational(T) & l, one_t ); // => ++?, ?++ 66 Rational(T) ??( Rational(T) l, Rational(T) r ); 67 Rational(T) ?=?( Rational(T) & l, Rational(T) r ); 68 Rational(T) ?=?( Rational(T) & l, one_t ); // => ?, ? 69 Rational(T) ?*?( Rational(T) l, Rational(T) r ); 70 Rational(T) ?*=?( Rational(T) & l, Rational(T) r ); 71 Rational(T) ?/?( Rational(T) l, Rational(T) r ); 72 Rational(T) ?/=?( Rational(T) & l, Rational(T) r ); 89 73 90 74 // I/O 91 forall( istype &  istream( istype )  { istype & ??( istype &, RationalImpl& ); } )92 istype & ??( istype &, Rational( RationalImpl) & );75 forall( istype &  istream( istype )  { istype & ??( istype &, T & ); } ) 76 istype & ??( istype &, Rational(T) & ); 93 77 94 forall( ostype &  ostream( ostype )  { ostype & ??( ostype &, RationalImpl); } ) {95 ostype & ??( ostype &, Rational( RationalImpl) );96 void ??( ostype &, Rational( RationalImpl) );78 forall( ostype &  ostream( ostype )  { ostype & ??( ostype &, T ); } ) { 79 ostype & ??( ostype &, Rational(T) ); 80 void ??( ostype &, Rational(T) ); 97 81 } // distribution 98 82 } // distribution 99 83 100 forall( RationalImpl  arithmetic( RationalImpl ) {RationalImpl ?\?( RationalImpl, unsigned long );} ) 101 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ); 84 forall( T  Arithmetic( T )  { T ?\?( T, unsigned long ); } ) { 85 Rational(T) ?\?( Rational(T) x, long int y ); 86 Rational(T) ?\=?( Rational(T) & x, long int y ); 87 } // distribution 102 88 103 89 // conversion 104 forall( RationalImpl  arithmetic( RationalImpl )  { double convert( RationalImpl); } )105 double widen( Rational( RationalImpl) r );106 forall( RationalImpl  arithmetic( RationalImpl )  { double convert( RationalImpl ); RationalImplconvert( double );} )107 Rational( RationalImpl) narrow( double f, RationalImplmd );90 forall( T  Arithmetic( T )  { double convert( T ); } ) 91 double widen( Rational(T) r ); 92 forall( T  Arithmetic( T )  { double convert( T ); T convert( double );} ) 93 Rational(T) narrow( double f, T md ); 108 94 109 95 // Local Variables: //
Note: See TracChangeset
for help on using the changeset viewer.