Changes in libcfa/src/rational.hfa [5dc4c7e:fd54fef]
- File:
-
- 1 edited
-
libcfa/src/rational.hfa (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/rational.hfa
r5dc4c7e rfd54fef 12 12 // Created On : Wed Apr 6 17:56:25 2016 13 13 // Last Modified By : Peter A. Buhr 14 // Last Modified On : Tue Jul 20 17:45:29 202115 // Update Count : 1 1814 // Last Modified On : Tue Mar 26 23:16:10 2019 15 // Update Count : 109 16 16 // 17 17 … … 19 19 20 20 #include "iostream.hfa" 21 #include "math.trait.hfa" // Arithmetic 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 }; 22 45 23 46 // implementation 24 47 25 forall( T | Arithmetic( T) ) {48 forall( RationalImpl | arithmetic( RationalImpl ) ) { 26 49 struct Rational { 27 T numerator, denominator;// invariant: denominator > 050 RationalImpl numerator, denominator; // invariant: denominator > 0 28 51 }; // Rational 29 52 30 53 // constructors 31 54 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);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 ); 37 60 38 61 // numerator/denominator getter 39 62 40 T numerator( Rational(T) r );41 T denominator( Rational(T) r );42 [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src );63 RationalImpl numerator( Rational(RationalImpl) r ); 64 RationalImpl denominator( Rational(RationalImpl) r ); 65 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ); 43 66 44 67 // numerator/denominator setter 45 68 46 T numerator( Rational(T) r, Tn );47 T denominator( Rational(T) r, Td );69 RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ); 70 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ); 48 71 49 72 // comparison 50 73 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 ); 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 ); 58 80 59 81 // arithmetic 60 82 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 ); 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 ); 73 89 74 90 // I/O 75 forall( istype & | istream( istype ) | { istype & ?|?( istype &, T& ); } )76 istype & ?|?( istype &, Rational( T) & );91 forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } ) 92 istype & ?|?( istype &, Rational(RationalImpl) & ); 77 93 78 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T); } ) {79 ostype & ?|?( ostype &, Rational( T) );80 void ?|?( ostype &, Rational( T) );94 forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) { 95 ostype & ?|?( ostype &, Rational(RationalImpl) ); 96 void ?|?( ostype &, Rational(RationalImpl) ); 81 97 } // distribution 82 98 } // distribution 83 99 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 100 forall( RationalImpl | arithmetic( RationalImpl ) |{RationalImpl ?\?( RationalImpl, unsigned long );} ) 101 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ); 88 102 89 103 // conversion 90 forall( T | Arithmetic( T ) | { double convert( T); } )91 double widen( Rational( T) r );92 forall( T | Arithmetic( T ) | { double convert( T ); Tconvert( double );} )93 Rational( T) narrow( double f, Tmd );104 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } ) 105 double widen( Rational(RationalImpl) r ); 106 forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double );} ) 107 Rational(RationalImpl) narrow( double f, RationalImpl md ); 94 108 95 109 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.