source: src/libcfa/rational.c @ babeeda

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since babeeda was 09687aa, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

complete conversion of iostream/fstream to use references

  • Property mode set to 100644
File size: 7.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// rational.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Wed Apr  6 17:54:28 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Dec  6 23:13:58 2017
13// Update Count     : 156
14//
15
16#include "rational"
17#include "fstream"
18#include "stdlib"
19
20// helper routines
21
22// Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce rationals.
23// alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
24forall( otype RationalImpl | arithmetic( RationalImpl ) )
25static RationalImpl gcd( RationalImpl a, RationalImpl b ) {
26        for ( ;; ) {                                                                            // Euclid's algorithm
27                RationalImpl r = a % b;
28          if ( r == (RationalImpl){0} ) break;
29                a = b;
30                b = r;
31        } // for
32        return b;
33} // gcd
34
35forall( otype RationalImpl | arithmetic( RationalImpl ) )
36static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) {
37        if ( d == (RationalImpl){0} ) {
38                serr | "Invalid rational number construction: denominator cannot be equal to 0." | endl;
39                exit( EXIT_FAILURE );
40        } // exit
41        if ( d < (RationalImpl){0} ) { d = -d; n = -n; }        // move sign to numerator
42        return gcd( abs( n ), d );                                                      // simplify
43} // Rationalnumber::simplify
44
45
46// constructors
47
48forall( otype RationalImpl | arithmetic( RationalImpl ) )
49void ?{}( Rational(RationalImpl) & r ) {
50        r{ (RationalImpl){0}, (RationalImpl){1} };
51} // rational
52
53forall( otype RationalImpl | arithmetic( RationalImpl ) )
54void ?{}( Rational(RationalImpl) & r, RationalImpl n ) {
55        r{ n, (RationalImpl){1} };
56} // rational
57
58forall( otype RationalImpl | arithmetic( RationalImpl ) )
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;
63} // rational
64
65
66// getter for numerator/denominator
67
68forall( otype RationalImpl | arithmetic( RationalImpl ) )
69RationalImpl numerator( Rational(RationalImpl) r ) {
70        return r.numerator;
71} // numerator
72
73forall( otype RationalImpl | arithmetic( RationalImpl ) )
74RationalImpl denominator( Rational(RationalImpl) r ) {
75        return r.denominator;
76} // denominator
77
78forall( otype RationalImpl | arithmetic( RationalImpl ) )
79[ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
80        return dest = src.[ numerator, denominator ];
81}
82
83// setter for numerator/denominator
84
85forall( otype RationalImpl | arithmetic( RationalImpl ) )
86RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
87        RationalImpl prev = r.numerator;
88        RationalImpl t = gcd( abs( n ), r.denominator );                // simplify
89        r.numerator = n / t;
90        r.denominator = r.denominator / t;
91        return prev;
92} // numerator
93
94forall( otype RationalImpl | arithmetic( RationalImpl ) )
95RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
96        RationalImpl prev = r.denominator;
97        RationalImpl t = simplify( r.numerator, d );                    // simplify
98        r.numerator = r.numerator / t;
99        r.denominator = d / t;
100        return prev;
101} // denominator
102
103
104// comparison
105
106forall( otype RationalImpl | arithmetic( RationalImpl ) )
107int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
108        return l.numerator * r.denominator == l.denominator * r.numerator;
109} // ?==?
110
111forall( otype RationalImpl | arithmetic( RationalImpl ) )
112int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
113        return ! ( l == r );
114} // ?!=?
115
116forall( otype RationalImpl | arithmetic( RationalImpl ) )
117int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
118        return l.numerator * r.denominator < l.denominator * r.numerator;
119} // ?<?
120
121forall( otype RationalImpl | arithmetic( RationalImpl ) )
122int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
123        return l.numerator * r.denominator <= l.denominator * r.numerator;
124} // ?<=?
125
126forall( otype RationalImpl | arithmetic( RationalImpl ) )
127int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
128        return ! ( l <= r );
129} // ?>?
130
131forall( otype RationalImpl | arithmetic( RationalImpl ) )
132int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
133        return ! ( l < r );
134} // ?>=?
135
136
137// arithmetic
138
139forall( otype RationalImpl | arithmetic( RationalImpl ) )
140Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
141        Rational(RationalImpl) t = { r.numerator, r.denominator };
142        return t;
143} // +?
144
145forall( otype RationalImpl | arithmetic( RationalImpl ) )
146Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
147        Rational(RationalImpl) t = { -r.numerator, r.denominator };
148        return t;
149} // -?
150
151forall( otype RationalImpl | arithmetic( RationalImpl ) )
152Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
153        if ( l.denominator == r.denominator ) {                         // special case
154                Rational(RationalImpl) t = { l.numerator + r.numerator, l.denominator };
155                return t;
156        } else {
157                Rational(RationalImpl) t = { l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
158                return t;
159        } // if
160} // ?+?
161
162forall( otype RationalImpl | arithmetic( RationalImpl ) )
163Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
164        if ( l.denominator == r.denominator ) {                         // special case
165                Rational(RationalImpl) t = { l.numerator - r.numerator, l.denominator };
166                return t;
167        } else {
168                Rational(RationalImpl) t = { l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
169                return t;
170        } // if
171} // ?-?
172
173forall( otype RationalImpl | arithmetic( RationalImpl ) )
174Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
175        Rational(RationalImpl) t = { l.numerator * r.numerator, l.denominator * r.denominator };
176        return t;
177} // ?*?
178
179forall( otype RationalImpl | arithmetic( RationalImpl ) )
180Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
181        if ( r.numerator < (RationalImpl){0} ) {
182                r.numerator = -r.numerator;
183                r.denominator = -r.denominator;
184        } // if
185        Rational(RationalImpl) t = { l.numerator * r.denominator, l.denominator * r.numerator };
186        return t;
187} // ?/?
188
189
190// conversion
191
192forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
193double widen( Rational(RationalImpl) r ) {
194        return convert( r.numerator ) / convert( r.denominator );
195} // widen
196
197// http://www.ics.uci.edu/~eppstein/numth/frap.c
198forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
199Rational(RationalImpl) narrow( double f, RationalImpl md ) {
200        if ( md <= (RationalImpl){1} ) {                                        // maximum fractional digits too small?
201                return (Rational(RationalImpl)){ convert( f ), (RationalImpl){1}}; // truncate fraction
202        } // if
203
204        // continued fraction coefficients
205        RationalImpl m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 };
206        RationalImpl ai, t;
207
208        // find terms until denom gets too big
209        for ( ;; ) {
210                ai = convert( f );
211          if ( ! (m10 * ai + m11 <= md) ) break;
212                t = m00 * ai + m01;
213                m01 = m00;
214                m00 = t;
215                t = m10 * ai + m11;
216                m11 = m10;
217                m10 = t;
218                double temp = convert( ai );
219          if ( f == temp ) break;                                                       // prevent division by zero
220                f = 1 / (f - temp);
221          if ( f > (double)0x7FFFFFFF ) break;                          // representation failure
222        } // for
223        return (Rational(RationalImpl)){ m00, m10 };
224} // narrow
225
226
227// I/O
228
229forall( otype RationalImpl | arithmetic( RationalImpl ) )
230forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
231istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
232        RationalImpl t;
233        is | r.numerator | r.denominator;
234        t = simplify( r.numerator, r.denominator );
235        r.numerator /= t;
236        r.denominator /= t;
237        return is;
238} // ?|?
239
240forall( otype RationalImpl | arithmetic( RationalImpl ) )
241forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } )
242ostype & ?|?( ostype & os, Rational(RationalImpl ) r ) {
243        return os | r.numerator | '/' | r.denominator;
244} // ?|?
245
246// Local Variables: //
247// tab-width: 4 //
248// End: //
Note: See TracBrowser for help on using the repository browser.