Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/rational.c

    r630a82a r3d9b5da  
    1111// Created On       : Wed Apr  6 17:54:28 2016
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Fri Apr  8 15:39:17 2016
    14 // Update Count     : 17
     13// Last Modified On : Thu Apr  7 17:28:03 2016
     14// Update Count     : 12
    1515//
    1616
     
    2323} // extern
    2424
    25 
    26 // constants
    27 
    2825struct Rational 0 = {0, 1};
    2926struct Rational 1 = {1, 1};
    3027
    31 
    32 // helper
    33 
    34 // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
    35 static long int gcd( long int a, long int b ) {
     28// Calculate the greatest common denominator of two numbers, the first of which may be negative.  It is used to reduce
     29// rationals.
     30
     31long int gcd( long int a, long int b ) {
    3632    for ( ;; ) {                                                                                // Euclid's algorithm
    3733                long int r = a % b;
     
    4339} // gcd
    4440
    45 static long int simplify( long int *n, long int *d ) {
     41long int simplify( long int *n, long int *d ) {
    4642    if ( *d == 0 ) {
    4743                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
     
    5248} // Rationalnumber::simplify
    5349
    54 
    55 // constructors
    56 
    57 Rational rational() {
    58     return (Rational){ 0, 1 };
    59 //      Rational t = { 0, 1 };
    60 //      return t;
     50Rational rational() {                                                                   // constructor
     51//    r = (Rational){ 0, 1 };
     52        Rational t = { 0, 1 };
     53        return t;
    6154} // rational
    6255
    63 Rational rational( long int n ) {
    64     return (Rational){ n, 1 };
    65 //      Rational t = { n, 1 };
    66 //      return t;
     56Rational rational( long int n ) {                                               // constructor
     57//    r = (Rational){ n, 1 };
     58        Rational t = { n, 1 };
     59        return t;
    6760} // rational
    6861
    69 Rational rational( long int n, long int d ) {
     62Rational rational( long int n, long int d ) {                   // constructor
    7063    long int t = simplify( &n, &d );                                    // simplify
    7164//    r = (Rational){ n / t, d / t };
     
    7366        return t;
    7467} // rational
    75 
    76 
    77 // getter/setter for numerator/denominator
    7868
    7969long int numerator( Rational r ) {
     
    8979} // numerator
    9080
    91 long int denominator( Rational r ) {
    92     return r.denominator;
    93 } // denominator
    94 
    9581long int denominator( Rational r, long int d ) {
    9682    long int prev = r.denominator;
     
    10187} // denominator
    10288
    103 
    104 // comparison
    105 
    10689int ?==?( Rational l, Rational r ) {
    10790    return l.numerator * r.denominator == l.denominator * r.numerator;
     
    127110    return ! ( l < r );
    128111} // ?>=?
    129 
    130 
    131 // arithmetic
    132112
    133113Rational -?( Rational r ) {
     
    169149    return t;
    170150} // ?/?
    171 
    172 
    173 // conversion
    174151
    175152double widen( Rational r ) {
     
    211188} // narrow
    212189
    213 
    214 // I/O
    215 
    216190forall( dtype istype | istream( istype ) )
    217191istype * ?|?( istype *is, Rational *r ) {
Note: See TracChangeset for help on using the changeset viewer.