Changes in / [13e2c54:0428aad]


Ignore:
Location:
src
Files:
2 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/rational

    r13e2c54 r0428aad  
    1212// Created On       : Wed Apr  6 17:56:25 2016
    1313// Last Modified By : Peter A. Buhr
    14 // Last Modified On : Mon May  1 08:25:06 2017
    15 // Update Count     : 33
     14// Last Modified On : Wed May  4 14:11:45 2016
     15// Update Count     : 16
    1616//
    17 
    1817#ifndef RATIONAL_H
    1918#define RATIONAL_H
     
    2221
    2322// implementation
    24 typedef long int RationalImpl;
    2523struct Rational {
    26         RationalImpl numerator, denominator;                                    // invariant: denominator > 0
     24        long int numerator, denominator;                                        // invariant: denominator > 0
    2725}; // Rational
    2826
     
    3331// constructors
    3432void ?{}( Rational * r );
    35 void ?{}( Rational * r, RationalImpl n );
    36 void ?{}( Rational * r, RationalImpl n, RationalImpl d );
     33void ?{}( Rational * r, long int n );
     34void ?{}( Rational * r, long int n, long int d );
    3735
    38 // getter for numerator/denominator
    39 RationalImpl numerator( Rational r );
    40 RationalImpl denominator( Rational r );
    41 [ RationalImpl, RationalImpl ] ?=?( * [ RationalImpl, RationalImpl ] dest, Rational src );
    42 // setter for numerator/denominator
    43 RationalImpl numerator( Rational r, RationalImpl n );
    44 RationalImpl denominator( Rational r, RationalImpl d );
     36// getter/setter for numerator/denominator
     37long int numerator( Rational r );
     38long int numerator( Rational r, long int n );
     39long int denominator( Rational r );
     40long int denominator( Rational r, long int d );
    4541
    4642// comparison
     
    6157// conversion
    6258double widen( Rational r );
    63 Rational narrow( double f, RationalImpl md );
     59Rational narrow( double f, long int md );
    6460
    6561// I/O
  • src/libcfa/rational.c

    r13e2c54 r0428aad  
    1010// Created On       : Wed Apr  6 17:54:28 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr 27 17:05:06 2017
    13 // Update Count     : 51
     12// Last Modified On : Sat Jul  9 11:18:04 2016
     13// Update Count     : 40
    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 RationalImpl gcd( RationalImpl a, RationalImpl b ) {
     32static long int gcd( long int a, long int b ) {
    3333        for ( ;; ) {                                                                            // Euclid's algorithm
    34                 RationalImpl r = a % b;
     34                long int r = a % b;
    3535          if ( r == 0 ) break;
    3636                a = b;
     
    4040} // gcd
    4141
    42 static RationalImpl simplify( RationalImpl *n, RationalImpl *d ) {
     42static long int simplify( long int *n, long int *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, RationalImpl n ) {
     58void ?{}( Rational * r, long int n ) {
    5959        r{ n, 1 };
    6060} // rational
    6161
    62 void ?{}( Rational * r, RationalImpl n, RationalImpl d ) {
    63         RationalImpl t = simplify( &n, &d );                            // simplify
     62void ?{}( Rational * r, long int n, long int d ) {
     63        long int t = simplify( &n, &d );                                        // simplify
    6464        r->numerator = n / t;
    6565        r->denominator = d / t;
     
    6767
    6868
    69 // getter for numerator/denominator
    70 
    71 RationalImpl numerator( Rational r ) {
     69// getter/setter for numerator/denominator
     70
     71long int numerator( Rational r ) {
    7272        return r.numerator;
    7373} // numerator
    7474
    75 RationalImpl 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 
    85 RationalImpl numerator( Rational r, RationalImpl n ) {
    86         RationalImpl prev = r.numerator;
    87         RationalImpl t = gcd( abs( n ), r.denominator );                // simplify
     75long int numerator( Rational r, long int n ) {
     76        long int prev = r.numerator;
     77        long int t = gcd( abs( n ), r.denominator );            // simplify
    8878        r.numerator = n / t;
    8979        r.denominator = r.denominator / t;
     
    9181} // numerator
    9282
    93 RationalImpl denominator( Rational r, RationalImpl d ) {
    94         RationalImpl prev = r.denominator;
    95         RationalImpl t = simplify( &r.numerator, &d );                  // simplify
     83long int denominator( Rational r ) {
     84        return r.denominator;
     85} // denominator
     86
     87long int denominator( Rational r, long int d ) {
     88        long int prev = r.denominator;
     89        long int t = simplify( &r.numerator, &d );                      // simplify
    9690        r.numerator = r.numerator / t;
    9791        r.denominator = d / t;
     
    176170
    177171// http://www.ics.uci.edu/~eppstein/numth/frap.c
    178 Rational narrow( double f, RationalImpl md ) {
     172Rational narrow( double f, long int md ) {
    179173        if ( md <= 1 ) {                                                                        // maximum fractional digits too small?
    180174                return (Rational){ f, 1};                                               // truncate fraction
     
    182176
    183177        // continued fraction coefficients
    184         RationalImpl m00 = 1, m11 = 1, m01 = 0, m10 = 0;
    185         RationalImpl ai, t;
     178        long int m00 = 1, m11 = 1, m01 = 0, m10 = 0;
     179        long int ai, t;
    186180
    187181        // find terms until denom gets too big
    188182        for ( ;; ) {
    189                 ai = (RationalImpl)f;
     183                ai = (long int)f;
    190184          if ( ! (m10 * ai + m11 <= md) ) break;
    191185                t = m00 * ai + m01;
     
    208202forall( dtype istype | istream( istype ) )
    209203istype * ?|?( istype *is, Rational *r ) {
    210         RationalImpl t;
     204        long int t;
    211205        is | &(r->numerator) | &(r->denominator);
    212206        t = simplify( &(r->numerator), &(r->denominator) );
  • src/tests/rational.c

    r13e2c54 r0428aad  
    1010// Created On       : Mon Mar 28 08:43:12 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Apr 27 17:05:19 2017
    13 // Update Count     : 40
     12// Last Modified On : Tue Jul  5 18:29:37 2016
     13// Update Count     : 25
    1414//
    1515
     
    3636        b = (Rational){ -3, 2 };
    3737        sout | a | b | endl;
    38 //      sout | a == 1 | endl; // FIX ME
     38        sout | a == 1 | endl;
    3939        sout | a != b | endl;
    4040        sout | a <  b | endl;
     
    6161        sout | narrow( 3.14159265358979, 256 ) | endl;
    6262
    63         sout | "decompose" | endl;
    64         RationalImpl n, d;
    65         [n, d] = a;
    66         sout | a | n | d | endl;
    67 
    68         sout | "more tests" | endl;
    6963        Rational x = { 1, 2 }, y = { 2 };
    7064        sout | x - y | endl;
Note: See TracChangeset for help on using the changeset viewer.