# source:libcfa/src/rational.cfa@fd54fef

Last change on this file since fd54fef was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 2 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

• Property mode set to `100644`
File size: 7.3 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 : Sat Feb  8 17:56:36 2020
13// Update Count     : 187
14//
15
16#include "rational.hfa"
17#include "fstream.hfa"
18#include "stdlib.hfa"
19
20forall( 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        void ?{}( Rational(RationalImpl) & r, zero_t ) {
59                r{ (RationalImpl){0}, (RationalImpl){1} };
60        } // rational
61
62        void ?{}( Rational(RationalImpl) & r, one_t ) {
63                r{ (RationalImpl){1}, (RationalImpl){1} };
64        } // rational
65
66        // getter for numerator/denominator
67
68        RationalImpl numerator( Rational(RationalImpl) r ) {
69                return r.numerator;
70        } // numerator
71
72        RationalImpl denominator( Rational(RationalImpl) r ) {
73                return r.denominator;
74        } // denominator
75
76        [ RationalImpl, RationalImpl ] ?=?( & [ RationalImpl, RationalImpl ] dest, Rational(RationalImpl) src ) {
77                return dest = src.[ numerator, denominator ];
78        } // ?=?
79
80        // setter for numerator/denominator
81
82        RationalImpl numerator( Rational(RationalImpl) r, RationalImpl n ) {
83                RationalImpl prev = r.numerator;
84                RationalImpl t = gcd( abs( n ), r.denominator ); // simplify
85                r.[numerator, denominator] = [n / t, r.denominator / t];
86                return prev;
87        } // numerator
88
89        RationalImpl denominator( Rational(RationalImpl) r, RationalImpl d ) {
90                RationalImpl prev = r.denominator;
91                RationalImpl t = simplify( r.numerator, d );    // simplify
92                r.[numerator, denominator] = [r.numerator / t, d / t];
93                return prev;
94        } // denominator
95
96        // comparison
97
98        int ?==?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
99                return l.numerator * r.denominator == l.denominator * r.numerator;
100        } // ?==?
101
102        int ?!=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
103                return ! ( l == r );
104        } // ?!=?
105
106        int ?<?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
107                return l.numerator * r.denominator < l.denominator * r.numerator;
108        } // ?<?
109
110        int ?<=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
111                return l.numerator * r.denominator <= l.denominator * r.numerator;
112        } // ?<=?
113
114        int ?>?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
115                return ! ( l <= r );
116        } // ?>?
117
118        int ?>=?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
119                return ! ( l < r );
120        } // ?>=?
121
122        // arithmetic
123
124        Rational(RationalImpl) +?( Rational(RationalImpl) r ) {
125                return (Rational(RationalImpl)){ r.numerator, r.denominator };
126        } // +?
127
128        Rational(RationalImpl) -?( Rational(RationalImpl) r ) {
129                return (Rational(RationalImpl)){ -r.numerator, r.denominator };
130        } // -?
131
132        Rational(RationalImpl) ?+?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
133                if ( l.denominator == r.denominator ) {                 // special case
134                        return (Rational(RationalImpl)){ l.numerator + r.numerator, l.denominator };
135                } else {
136                        return (Rational(RationalImpl)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator };
137                } // if
138        } // ?+?
139
140        Rational(RationalImpl) ?-?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
141                if ( l.denominator == r.denominator ) {                 // special case
142                        return (Rational(RationalImpl)){ l.numerator - r.numerator, l.denominator };
143                } else {
144                        return (Rational(RationalImpl)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator };
145                } // if
146        } // ?-?
147
148        Rational(RationalImpl) ?*?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
149                return (Rational(RationalImpl)){ l.numerator * r.numerator, l.denominator * r.denominator };
150        } // ?*?
151
152        Rational(RationalImpl) ?/?( Rational(RationalImpl) l, Rational(RationalImpl) r ) {
153                if ( r.numerator < (RationalImpl){0} ) {
154                        r.[numerator, denominator] = [-r.numerator, -r.denominator];
155                } // if
156                return (Rational(RationalImpl)){ l.numerator * r.denominator, l.denominator * r.numerator };
157        } // ?/?
158
159        // I/O
160
161        forall( istype & | istream( istype ) | { istype & ?|?( istype &, RationalImpl & ); } )
162        istype & ?|?( istype & is, Rational(RationalImpl) & r ) {
163                is | r.numerator | r.denominator;
164                RationalImpl t = simplify( r.numerator, r.denominator );
165                r.numerator /= t;
166                r.denominator /= t;
167                return is;
168        } // ?|?
169
170        forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, RationalImpl ); } ) {
171                ostype & ?|?( ostype & os, Rational(RationalImpl) r ) {
172                        return os | r.numerator | '/' | r.denominator;
173                } // ?|?
174
175                void ?|?( ostype & os, Rational(RationalImpl) r ) {
176                        (ostype &)(os | r); ends( os );
177                } // ?|?
178        } // distribution
179} // distribution
180
181forall( RationalImpl | arithmetic( RationalImpl ) | { RationalImpl ?\?( RationalImpl, unsigned long ); } )
182Rational(RationalImpl) ?\?( Rational(RationalImpl) x, long int y ) {
183        if ( y < 0 ) {
184                return (Rational(RationalImpl)){ x.denominator \ -y, x.numerator \ -y };
185        } else {
186                return (Rational(RationalImpl)){ x.numerator \ y, x.denominator \ y };
187        } // if
188}
189
190// conversion
191
192forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); } )
193double widen( Rational(RationalImpl) r ) {
194        return convert( r.numerator ) / convert( r.denominator );
195} // widen
196
197forall( RationalImpl | arithmetic( RationalImpl ) | { double convert( RationalImpl ); RationalImpl convert( double ); } )
198Rational(RationalImpl) narrow( double f, RationalImpl md ) {
199        // http://www.ics.uci.edu/~eppstein/numth/frap.c
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// Local Variables: //
227// tab-width: 4 //
228// End: //
Note: See TracBrowser for help on using the repository browser.