Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/rational.c

    r53a6c2a r7bc4e6b  
    1 // 
     1//
    22// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
    33//
    44// The contents of this file are covered under the licence agreement in the
    55// file "LICENCE" distributed with Cforall.
    6 // 
    7 // rational.c -- 
    8 // 
     6//
     7// rational.c --
     8//
    99// Author           : Peter A. Buhr
    1010// Created On       : Wed Apr  6 17:54:28 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue May 16 18:35:36 2017
    13 // Update Count     : 150
    14 // 
     12// Last Modified On : Wed Aug 23 22:38:48 2017
     13// Update Count     : 154
     14//
    1515
    1616#include "rational"
     
    3434
    3535forall( otype RationalImpl | arithmetic( RationalImpl ) )
    36 static RationalImpl simplify( RationalImpl * n, RationalImpl * d ) {
    37         if ( *d == (RationalImpl){0} ) {
     36static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) {
     37        if ( d == (RationalImpl){0} ) {
    3838                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
    3939                exit( EXIT_FAILURE );
    4040        } // exit
    41         if ( *d < (RationalImpl){0} ) { *d = -*d; *n = -*n; } // move sign to numerator
    42         return gcd( abs( *n ), *d );                                            // simplify
     41        if ( d < (RationalImpl){0} ) { d = -d; n = -n; }        // move sign to numerator
     42        return gcd( abs( n ), d );                                                      // simplify
    4343} // Rationalnumber::simplify
    4444
     
    4747
    4848forall( otype RationalImpl | arithmetic( RationalImpl ) )
    49 void ?{}( Rational(RationalImpl) * r ) {
     49void ?{}( Rational(RationalImpl) & r ) {
    5050        r{ (RationalImpl){0}, (RationalImpl){1} };
    5151} // rational
    5252
    5353forall( otype RationalImpl | arithmetic( RationalImpl ) )
    54 void ?{}( Rational(RationalImpl) * r, RationalImpl n ) {
     54void ?{}( Rational(RationalImpl) & r, RationalImpl n ) {
    5555        r{ n, (RationalImpl){1} };
    5656} // rational
    5757
    5858forall( otype RationalImpl | arithmetic( RationalImpl ) )
    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;
     59void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) {
     60        RationalImpl t = simplify( n, d );                                      // simplify
     61        r.numerator = n / t;
     62        r.denominator = d / t;
    6363} // rational
    6464
     
    7777
    7878forall( 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 ];
    8181}
    8282
     
    9595RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
    9696        RationalImpl prev = r.denominator;
    97         RationalImpl t = simplify( &r.numerator, &d );                  // simplify
     97        RationalImpl t = simplify( r.numerator, d );                    // simplify
    9898        r.numerator = r.numerator / t;
    9999        r.denominator = d / t;
     
    228228
    229229forall( otype RationalImpl | arithmetic( RationalImpl ) )
    230 forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl * ); } )
    231 istype * ?|?( istype * is, Rational(RationalImpl) * r ) {
     230forall( dtype istype | istream( istype ) | { istype * ?|?( istype *, RationalImpl & ); } )
     231istype * ?|?( istype * is, Rational(RationalImpl) & r ) {
    232232        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;
    237237        return is;
    238238} // ?|?
Note: See TracChangeset for help on using the changeset viewer.