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