Changeset 5544465 for src/libcfa


Ignore:
Timestamp:
May 1, 2017, 6:27:38 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
5783e94
Parents:
ad1a8dd (diff), 2055098 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/rational

    rad1a8dd r5544465  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Wed May  4 14:11:45 2016
    15 // Update Count     : 16
     14// Last Modified On : Mon May  1 08:25:06 2017
     15// Update Count     : 33
    1616//
     17
    1718#ifndef RATIONAL_H
    1819#define RATIONAL_H
     
    2122
    2223// implementation
     24typedef long int RationalImpl;
    2325struct Rational {
    24         long int numerator, denominator;                                        // invariant: denominator > 0
     26        RationalImpl numerator, denominator;                                    // invariant: denominator > 0
    2527}; // Rational
    2628
     
    3133// constructors
    3234void ?{}( Rational * r );
    33 void ?{}( Rational * r, long int n );
    34 void ?{}( Rational * r, long int n, long int d );
     35void ?{}( Rational * r, RationalImpl n );
     36void ?{}( Rational * r, RationalImpl n, RationalImpl d );
    3537
    36 // getter/setter for numerator/denominator
    37 long int numerator( Rational r );
    38 long int numerator( Rational r, long int n );
    39 long int denominator( Rational r );
    40 long int denominator( Rational r, long int d );
     38// getter for numerator/denominator
     39RationalImpl numerator( Rational r );
     40RationalImpl denominator( Rational r );
     41[ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src );
     42// setter for numerator/denominator
     43RationalImpl numerator( Rational r, RationalImpl n );
     44RationalImpl denominator( Rational r, RationalImpl d );
    4145
    4246// comparison
     
    5761// conversion
    5862double widen( Rational r );
    59 Rational narrow( double f, long int md );
     63Rational narrow( double f, RationalImpl md );
    6064
    6165// I/O
  • src/libcfa/rational.c

    rad1a8dd r5544465  
    1010// Created On       : Wed Apr  6 17:54:28 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Jul  9 11:18:04 2016
    13 // Update Count     : 40
     12// Last Modified On : Thu Apr 27 17:05:06 2017
     13// Update Count     : 51
    1414//
    1515
     
    3030// Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
    3131// alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
    32 static long int gcd( long int a, long int b ) {
     32static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
    3333        for ( ;; ) {                                                                            // Euclid's algorithm
    34                 long int r = a % b;
     34                RationalImpl r = a % b;
    3535          if ( r == 0 ) break;
    3636                a = b;
     
    4040} // gcd
    4141
    42 static long int simplify( long int *n, long int *d ) {
     42static RationalImpl simplify( RationalImpl *n, RationalImpl *d ) {
    4343        if ( *d == 0 ) {
    4444                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
     
    5656} // rational
    5757
    58 void ?{}( Rational * r, long int n ) {
     58void ?{}( Rational * r, RationalImpl n ) {
    5959        r{ n, 1 };
    6060} // rational
    6161
    62 void ?{}( Rational * r, long int n, long int d ) {
    63         long int t = simplify( &n, &d );                                        // simplify
     62void ?{}( Rational * r, RationalImpl n, RationalImpl d ) {
     63        RationalImpl t = simplify( &n, &d );                            // simplify
    6464        r->numerator = n / t;
    6565        r->denominator = d / t;
     
    6767
    6868
    69 // getter/setter for numerator/denominator
    70 
    71 long int numerator( Rational r ) {
     69// getter for numerator/denominator
     70
     71RationalImpl numerator( Rational r ) {
    7272        return r.numerator;
    7373} // numerator
    7474
    75 long int numerator( Rational r, long int n ) {
    76         long int prev = r.numerator;
    77         long int t = gcd( abs( n ), r.denominator );            // simplify
     75RationalImpl denominator( Rational r ) {
     76        return r.denominator;
     77} // denominator
     78
     79[ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src ) {
     80        return *dest = src.[ numerator, denominator ];
     81}
     82
     83// setter for numerator/denominator
     84
     85RationalImpl numerator( Rational r, RationalImpl n ) {
     86        RationalImpl prev = r.numerator;
     87        RationalImpl t = gcd( abs( n ), r.denominator );                // simplify
    7888        r.numerator = n / t;
    7989        r.denominator = r.denominator / t;
     
    8191} // numerator
    8292
    83 long int denominator( Rational r ) {
    84         return r.denominator;
    85 } // denominator
    86 
    87 long int denominator( Rational r, long int d ) {
    88         long int prev = r.denominator;
    89         long int t = simplify( &r.numerator, &d );                      // simplify
     93RationalImpl denominator( Rational r, RationalImpl d ) {
     94        RationalImpl prev = r.denominator;
     95        RationalImpl t = simplify( &r.numerator, &d );                  // simplify
    9096        r.numerator = r.numerator / t;
    9197        r.denominator = d / t;
     
    170176
    171177// http://www.ics.uci.edu/~eppstein/numth/frap.c
    172 Rational narrow( double f, long int md ) {
     178Rational narrow( double f, RationalImpl md ) {
    173179        if ( md <= 1 ) {                                                                        // maximum fractional digits too small?
    174180                return (Rational){ f, 1};                                               // truncate fraction
     
    176182
    177183        // continued fraction coefficients
    178         long int m00 = 1, m11 = 1, m01 = 0, m10 = 0;
    179         long int ai, t;
     184        RationalImpl m00 = 1, m11 = 1, m01 = 0, m10 = 0;
     185        RationalImpl ai, t;
    180186
    181187        // find terms until denom gets too big
    182188        for ( ;; ) {
    183                 ai = (long int)f;
     189                ai = (RationalImpl)f;
    184190          if ( ! (m10 * ai + m11 <= md) ) break;
    185191                t = m00 * ai + m01;
     
    202208forall( dtype istype | istream( istype ) )
    203209istype * ?|?( istype *is, Rational *r ) {
    204         long int t;
     210        RationalImpl t;
    205211        is | &(r->numerator) | &(r->denominator);
    206212        t = simplify( &(r->numerator), &(r->denominator) );
Note: See TracChangeset for help on using the changeset viewer.