source: libcfa/src/rational.cfa @ ff2a33e

arm-ehjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since ff2a33e was ff2a33e, checked in by Peter A. Buhr <pabuhr@…>, 2 years ago

switch from calling abort to using abort stream

  • Property mode set to 100644
File size: 7.1 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 : Fri Jul 12 18:12:08 2019
13// Update Count     : 184
14//
15
16#include "rational.hfa"
17#include "fstream.hfa"
18#include "stdlib.hfa"
19
20forall( otype RationalImpl | arithmetic( RationalImpl ) ) {
21        // helper routines
22
23        // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce
24        // rationals.  alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm
25        static 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
35        static RationalImpl simplify( RationalImpl & n, RationalImpl & d ) {
36                if ( d == (RationalImpl){0} ) {
37                        abort | "Invalid rational number construction: denominator cannot be equal to 0.";
38                } // exit
39                if ( d < (RationalImpl){0} ) { d = -d; n = -n; } // move sign to numerator
40                return gcd( abs( n ), d );                                              // simplify
41        } // Rationalnumber::simplify
42
43        // constructors
44
45        void ?{}( Rational(RationalImpl) & r ) {
46                r{ (RationalImpl){0}, (RationalImpl){1} };
47        } // rational
48
49        void ?{}( Rational(RationalImpl) & r, RationalImpl n ) {
50                r{ n, (RationalImpl){1} };
51        } // rational
52
53        void ?{}( Rational(RationalImpl) & r, RationalImpl n, RationalImpl d ) {
54                RationalImpl t = simplify( n, d );                              // simplify
55                r.[numerator, denominator] = [n / t, d / t];
56        } // rational
57
58
59        // getter for numerator/denominator
60
61        RationalImpl numerator( Rational(RationalImpl) r ) {
62                return r.numerator;
63        } // numerator
64
65        RationalImpl denominator( Rational(RationalImpl) r ) {
66                return r.denominator;
67        } // denominator
68
69        [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
70                return dest = src.[ numerator, denominator ];
71        } // ?=?
72
73        // setter for numerator/denominator
74
75        RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
76                RationalImpl prev = r.numerator;
77                RationalImpl t = gcd( abs( n ), r.denominator ); // simplify
78                r.[numerator, denominator] = [n / t, r.denominator / t];
79                return prev;
80        } // numerator
81
82        RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
83                RationalImpl prev = r.denominator;
84                RationalImpl t = simplify( r.numerator, d );    // simplify
85                r.[numerator, denominator] = [r.numerator / t, d / t];
86                return prev;
87        } // denominator
88
89        // comparison
90
91        int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
92                return l.numerator * r.denominator == l.denominator * r.numerator;
93        } // ?==?
94
95        int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
96                return ! ( l == r );
97        } // ?!=?
98
99        int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
100                return l.numerator * r.denominator < l.denominator * r.numerator;
101        } // ?<?
102
103        int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
104                return l.numerator * r.denominator <= l.denominator * r.numerator;
105        } // ?<=?
106
107        int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
108                return ! ( l <= r );
109        } // ?>?
110
111        int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
112                return ! ( l < r );
113        } // ?>=?
114
115        // arithmetic
116
117        Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
118                return (Rational(RationalImpl)){ r.numerator, r.denominator };
119        } // +?
120
121        Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
122                return (Rational(RationalImpl)){ -r.numerator, r.denominator };
123        } // -?
124
125        Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
126                if ( l.denominator == r.denominator ) {                 // special case
127                        return (Rational(RationalImpl)){ l.numerator + r.numerator, l.denominator };
128                } else {
129                        return (Rational(RationalImpl)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
130                } // if
131        } // ?+?
132
133        Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
134                if ( l.denominator == r.denominator ) {                 // special case
135                        return (Rational(RationalImpl)){ l.numerator - r.numerator, l.denominator };
136                } else {
137                        return (Rational(RationalImpl)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
138                } // if
139        } // ?-?
140
141        Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
142                return (Rational(RationalImpl)){ l.numerator * r.numerator, l.denominator * r.denominator };
143        } // ?*?
144
145        Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
146                if ( r.numerator < (RationalImpl){0} ) {
147                        r.[numerator, denominator] = [-r.numerator, -r.denominator];
148                } // if
149                return (Rational(RationalImpl)){ l.numerator * r.denominator, l.denominator * r.numerator };
150        } // ?/?
151
152        // I/O
153
154        forall( dtype istype | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
155        istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
156                is | r.numerator | r.denominator;
157                RationalImpl t = simplify( r.numerator, r.denominator );
158                r.numerator /= t;
159                r.denominator /= t;
160                return is;
161        } // ?|?
162
163        forall( dtype ostype | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
164                ostype & ?|?( ostype & os, Rational(RationalImpl) r ) {
165                        return os | r.numerator | '/' | r.denominator;
166                } // ?|?
167
168                void ?|?( ostype & os, Rational(RationalImpl) r ) {
169                        (ostype &)(os | r); ends( os );
170                } // ?|?
171        } // distribution
172} // distribution
173
174forall( otype RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } )
175Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) {
176        if ( y < 0 ) {
177                return (Rational(RationalImpl)){ x.denominator \ -y, x.numerator \ -y };
178        } else {
179                return (Rational(RationalImpl)){ x.numerator \ y, x.denominator \ y };
180        } // if
181}
182
183// conversion
184
185forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
186double widen( Rational(RationalImpl) r ) {
187        return convert( r.numerator ) / convert( r.denominator );
188} // widen
189
190forall( otype RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
191Rational(RationalImpl) narrow( double f, RationalImpl md ) {
192        // http://www.ics.uci.edu/~eppstein/numth/frap.c
193        if ( md <= (RationalImpl){1} ) {                                        // maximum fractional digits too small?
194                return (Rational(RationalImpl)){ convert( f ), (RationalImpl){1}}; // truncate fraction
195        } // if
196
197        // continued fraction coefficients
198        RationalImpl m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 };
199        RationalImpl ai, t;
200
201        // find terms until denom gets too big
202        for ( ;; ) {
203                ai = convert( f );
204          if ( ! (m10 * ai + m11 <= md) ) break;
205                t = m00 * ai + m01;
206                m01 = m00;
207                m00 = t;
208                t = m10 * ai + m11;
209                m11 = m10;
210                m10 = t;
211                double temp = convert( ai );
212          if ( f == temp ) break;                                                       // prevent division by zero
213                f = 1 / (f - temp);
214          if ( f > (double)0x7FFFFFFF ) break;                          // representation failure
215        } // for
216        return (Rational(RationalImpl)){ m00, m10 };
217} // narrow
218
219// Local Variables: //
220// tab-width: 4 //
221// End: //
Note: See TracBrowser for help on using the repository browser.