Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/rational.c

    r3d9b5da r9827c7ba  
    1111// Created On       : Wed Apr  6 17:54:28 2016
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Thu Apr  7 17:28:03 2016
    14 // Update Count     : 12
     13// Last Modified On : Fri Apr  8 17:35:05 2016
     14// Update Count     : 18
    1515//
    1616
     
    2323} // extern
    2424
     25
     26// constants
     27
    2528struct Rational 0 = {0, 1};
    2629struct Rational 1 = {1, 1};
    2730
    28 // Calculate the greatest common denominator of two numbers, the first of which may be negative.  It is used to reduce
    29 // rationals.
    30 
    31 long int gcd( long int a, long int b ) {
     31
     32// helper
     33
     34// Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
     35static long int gcd( long int a, long int b ) {
    3236    for ( ;; ) {                                                                                // Euclid's algorithm
    3337                long int r = a % b;
     
    3943} // gcd
    4044
    41 long int simplify( long int *n, long int *d ) {
     45static long int simplify( long int *n, long int *d ) {
    4246    if ( *d == 0 ) {
    4347                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
     
    4852} // Rationalnumber::simplify
    4953
    50 Rational rational() {                                                                   // constructor
    51 //    r = (Rational){ 0, 1 };
    52         Rational t = { 0, 1 };
    53         return t;
     54
     55// constructors
     56
     57Rational rational() {
     58    return (Rational){ 0, 1 };
    5459} // rational
    5560
    56 Rational rational( long int n ) {                                               // constructor
    57 //    r = (Rational){ n, 1 };
    58         Rational t = { n, 1 };
    59         return t;
     61Rational rational( long int n ) {
     62    return (Rational){ n, 1 };
    6063} // rational
    6164
    62 Rational rational( long int n, long int d ) {                   // constructor
     65Rational rational( long int n, long int d ) {
    6366    long int t = simplify( &n, &d );                                    // simplify
    64 //    r = (Rational){ n / t, d / t };
    65         Rational t = { n / t, d / t };
    66         return t;
     67    return (Rational){ n / t, d / t };
    6768} // rational
     69
     70
     71// getter/setter for numerator/denominator
    6872
    6973long int numerator( Rational r ) {
     
    7983} // numerator
    8084
     85long int denominator( Rational r ) {
     86    return r.denominator;
     87} // denominator
     88
    8189long int denominator( Rational r, long int d ) {
    8290    long int prev = r.denominator;
     
    8795} // denominator
    8896
     97
     98// comparison
     99
    89100int ?==?( Rational l, Rational r ) {
    90101    return l.numerator * r.denominator == l.denominator * r.numerator;
     
    110121    return ! ( l < r );
    111122} // ?>=?
     123
     124
     125// arithmetic
    112126
    113127Rational -?( Rational r ) {
     
    149163    return t;
    150164} // ?/?
     165
     166
     167// conversion
    151168
    152169double widen( Rational r ) {
     
    188205} // narrow
    189206
     207
     208// I/O
     209
    190210forall( dtype istype | istream( istype ) )
    191211istype * ?|?( istype *is, Rational *r ) {
Note: See TracChangeset for help on using the changeset viewer.