Changes in src/libcfa/rational.c [53a6c2a:7bc4e6b]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/rational.c
r53a6c2a r7bc4e6b 1 // 1 // 2 2 // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 3 3 // 4 4 // The contents of this file are covered under the licence agreement in the 5 5 // file "LICENCE" distributed with Cforall. 6 // 7 // rational.c -- 8 // 6 // 7 // rational.c -- 8 // 9 9 // Author : Peter A. Buhr 10 10 // Created On : Wed Apr 6 17:54:28 2016 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Tue May 16 18:35:36201713 // Update Count : 15 014 // 12 // Last Modified On : Wed Aug 23 22:38:48 2017 13 // Update Count : 154 14 // 15 15 16 16 #include "rational" … … 34 34 35 35 forall( otype RationalImpl | arithmetic( RationalImpl ) ) 36 static RationalImpl simplify( RationalImpl * n, RationalImpl *d ) {37 if ( *d == (RationalImpl){0} ) {36 static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) { 37 if ( d == (RationalImpl){0} ) { 38 38 serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl; 39 39 exit( EXIT_FAILURE ); 40 40 } // exit 41 if ( *d < (RationalImpl){0} ) { *d = -*d; *n = -*n; }// move sign to numerator42 return gcd( abs( *n ), *d );// simplify41 if ( d < (RationalImpl){0} ) { d = -d; n = -n; } // move sign to numerator 42 return gcd( abs( n ), d ); // simplify 43 43 } // Rationalnumber::simplify 44 44 … … 47 47 48 48 forall( otype RationalImpl | arithmetic( RationalImpl ) ) 49 void ?{}( Rational(RationalImpl) *r ) {49 void ?{}( Rational(RationalImpl) & r ) { 50 50 r{ (RationalImpl){0}, (RationalImpl){1} }; 51 51 } // rational 52 52 53 53 forall( otype RationalImpl | arithmetic( RationalImpl ) ) 54 void ?{}( Rational(RationalImpl) *r, RationalImpl n ) {54 void ?{}( Rational(RationalImpl) & r, RationalImpl n ) { 55 55 r{ n, (RationalImpl){1} }; 56 56 } // rational 57 57 58 58 forall( otype RationalImpl | arithmetic( RationalImpl ) ) 59 void ?{}( Rational(RationalImpl) *r, RationalImpl n, RationalImpl d ) {60 RationalImpl t = simplify( &n, &d );// simplify61 r ->numerator = n / t;62 r ->denominator = d / t;59 void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) { 60 RationalImpl t = simplify( n, d ); // simplify 61 r.numerator = n / t; 62 r.denominator = d / t; 63 63 } // rational 64 64 … … 77 77 78 78 forall( otype RationalImpl | arithmetic( RationalImpl ) ) 79 [ RationalImpl, RationalImpl ] ?=?( *[ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {80 return *dest = src.[ numerator, denominator ];79 [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) { 80 return dest = src.[ numerator, denominator ]; 81 81 } 82 82 … … 95 95 RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) { 96 96 RationalImpl prev = r.denominator; 97 RationalImpl t = simplify( &r.numerator, &d ); // simplify97 RationalImpl t = simplify( r.numerator, d ); // simplify 98 98 r.numerator = r.numerator / t; 99 99 r.denominator = d / t; … … 228 228 229 229 forall( otype RationalImpl | arithmetic( RationalImpl ) ) 230 forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl *); } )231 istype * ?|?( istype * is, Rational(RationalImpl) *r ) {230 forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl & ); } ) 231 istype * ?|?( istype * is, Rational(RationalImpl) & r ) { 232 232 RationalImpl t; 233 is | &(r->numerator) | &(r->denominator);234 t = simplify( &(r->numerator), &(r->denominator));235 r ->numerator /= t;236 r ->denominator /= t;233 is | r.numerator | r.denominator; 234 t = simplify( r.numerator, r.denominator ); 235 r.numerator /= t; 236 r.denominator /= t; 237 237 return is; 238 238 } // ?|?
Note:
See TracChangeset
for help on using the changeset viewer.