Changeset 0087e0e for libcfa/src/rational.cfa
 Timestamp:
 Mar 27, 2019, 8:58:53 AM (3 years ago)
 Branches:
 armeh, cleanupdtors, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr
 Children:
 1e5d0f0c
 Parents:
 8a30423
 gitauthor:
 Peter A. Buhr <pabuhr@…> (03/27/19 08:56:46)
 gitcommitter:
 Peter A. Buhr <pabuhr@…> (03/27/19 08:58:53)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

libcfa/src/rational.cfa
r8a30423 r0087e0e 10 10 // Created On : Wed Apr 6 17:54:28 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Dec 23 22:56:49 201813 // Update Count : 1 7012 // Last Modified On : Wed Mar 27 08:45:59 2019 13 // Update Count : 180 14 14 // 15 15 … … 54 54 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) { 55 55 RationalImpl t = simplify( n, d ); // simplify 56 r.numerator = n / t; 57 r.denominator = d / t; 56 r.[numerator, denominator] = [n / t, d / t]; 58 57 } // rational 59 58 … … 78 77 RationalImpl prev = r.numerator; 79 78 RationalImpl t = gcd( abs( n ), r.denominator ); // simplify 80 r.numerator = n / t; 81 r.denominator = r.denominator / t; 79 r.[numerator, denominator] = [n / t, r.denominator / t]; 82 80 return prev; 83 81 } // numerator … … 86 84 RationalImpl prev = r.denominator; 87 85 RationalImpl t = simplify( r.numerator, d ); // simplify 88 r.numerator = r.numerator / t; 89 r.denominator = d / t; 86 r.[numerator, denominator] = [r.numerator / t, d / t]; 90 87 return prev; 91 88 } // denominator … … 120 117 121 118 Rational(RationalImpl) +?( Rational(RationalImpl) r ) { 122 Rational(RationalImpl) t = { r.numerator, r.denominator }; 123 return t; 119 return (Rational(RationalImpl)){ r.numerator, r.denominator }; 124 120 } // +? 125 121 126 122 Rational(RationalImpl) ?( Rational(RationalImpl) r ) { 127 Rational(RationalImpl) t = { r.numerator, r.denominator }; 128 return t; 123 return (Rational(RationalImpl)){ r.numerator, r.denominator }; 129 124 } // ? 130 125 131 126 Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 132 127 if ( l.denominator == r.denominator ) { // special case 133 Rational(RationalImpl) t = { l.numerator + r.numerator, l.denominator }; 134 return t; 128 return (Rational(RationalImpl)){ l.numerator + r.numerator, l.denominator }; 135 129 } else { 136 Rational(RationalImpl) t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; 137 return t; 130 return (Rational(RationalImpl)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; 138 131 } // if 139 132 } // ?+? … … 141 134 Rational(RationalImpl) ??( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 142 135 if ( l.denominator == r.denominator ) { // special case 143 Rational(RationalImpl) t = { l.numerator  r.numerator, l.denominator }; 144 return t; 136 return (Rational(RationalImpl)){ l.numerator  r.numerator, l.denominator }; 145 137 } else { 146 Rational(RationalImpl) t = { l.numerator * r.denominator  l.denominator * r.numerator, l.denominator * r.denominator }; 147 return t; 138 return (Rational(RationalImpl)){ l.numerator * r.denominator  l.denominator * r.numerator, l.denominator * r.denominator }; 148 139 } // if 149 140 } // ?? 150 141 151 142 Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 152 Rational(RationalImpl) t = { l.numerator * r.numerator, l.denominator * r.denominator }; 153 return t; 143 return (Rational(RationalImpl)){ l.numerator * r.numerator, l.denominator * r.denominator }; 154 144 } // ?*? 155 145 156 146 Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) { 157 147 if ( r.numerator < (RationalImpl){0} ) { 158 r.numerator = r.numerator; 159 r.denominator = r.denominator; 148 r.[numerator, denominator] = [r.numerator, r.denominator]; 160 149 } // if 161 Rational(RationalImpl) t = { l.numerator * r.denominator, l.denominator * r.numerator }; 162 return t; 150 return (Rational(RationalImpl)){ l.numerator * r.denominator, l.denominator * r.numerator }; 163 151 } // ?/? 164 152 … … 167 155 forall( dtype istype  istream( istype )  { istype & ??( istype &, RationalImpl & ); } ) 168 156 istype & ??( istype & is, Rational(RationalImpl) & r ) { 169 RationalImpl t;170 157 is  r.numerator  r.denominator; 171 t = simplify( r.numerator, r.denominator );158 RationalImpl t = simplify( r.numerator, r.denominator ); 172 159 r.numerator /= t; 173 160 r.denominator /= t; … … 185 172 } // distribution 186 173 } // distribution 174 175 forall( otype RationalImpl  arithmetic( RationalImpl )  { RationalImpl ?\?( RationalImpl, unsigned long ); } ) 176 Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) { 177 if ( y < 0 ) { 178 return (Rational(RationalImpl)){ x.denominator \ y, x.numerator \ y }; 179 } else { 180 return (Rational(RationalImpl)){ x.numerator \ y, x.denominator \ y }; 181 } // if 182 } 187 183 188 184 // conversion
Note: See TracChangeset
for help on using the changeset viewer.