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 : Thu Aug 25 18:09:58 2022 |
---|
13 | // Update Count : 194 |
---|
14 | // |
---|
15 | |
---|
16 | #include "rational.hfa" |
---|
17 | #include "fstream.hfa" |
---|
18 | #include "stdlib.hfa" |
---|
19 | |
---|
20 | #pragma GCC visibility push(default) |
---|
21 | |
---|
22 | forall( T | Arithmetic( T ) ) { |
---|
23 | // helper routines |
---|
24 | |
---|
25 | // Calculate greatest common denominator of two numbers, the first of which may be negative. Used to reduce |
---|
26 | // rationals. alternative: https://en.wikipedia.org/wiki/Binary_GCD_algorithm |
---|
27 | static T gcd( T a, T b ) { |
---|
28 | for () { // Euclid's algorithm |
---|
29 | T r = a % b; |
---|
30 | if ( r == (T){0} ) break; |
---|
31 | a = b; |
---|
32 | b = r; |
---|
33 | } // for |
---|
34 | return b; |
---|
35 | } // gcd |
---|
36 | |
---|
37 | static T simplify( T & n, T & d ) { |
---|
38 | if ( d == (T){0} ) { |
---|
39 | abort | "Invalid rational number construction: denominator cannot be equal to 0."; |
---|
40 | } // exit |
---|
41 | if ( d < (T){0} ) { d = -d; n = -n; } // move sign to numerator |
---|
42 | return gcd( abs( n ), d ); // simplify |
---|
43 | } // Rationalnumber::simplify |
---|
44 | |
---|
45 | // constructors |
---|
46 | |
---|
47 | void ?{}( Rational(T) & r, zero_t ) { |
---|
48 | r{ (T){0}, (T){1} }; |
---|
49 | } // rational |
---|
50 | |
---|
51 | void ?{}( Rational(T) & r, one_t ) { |
---|
52 | r{ (T){1}, (T){1} }; |
---|
53 | } // rational |
---|
54 | |
---|
55 | void ?{}( Rational(T) & r ) { |
---|
56 | r{ (T){0}, (T){1} }; |
---|
57 | } // rational |
---|
58 | |
---|
59 | void ?{}( Rational(T) & r, T n ) { |
---|
60 | r{ n, (T){1} }; |
---|
61 | } // rational |
---|
62 | |
---|
63 | void ?{}( Rational(T) & r, T n, T d ) { |
---|
64 | T t = simplify( n, d ); // simplify |
---|
65 | r.[numerator, denominator] = [n / t, d / t]; |
---|
66 | } // rational |
---|
67 | |
---|
68 | // getter for numerator/denominator |
---|
69 | |
---|
70 | T numerator( Rational(T) r ) { |
---|
71 | return r.numerator; |
---|
72 | } // numerator |
---|
73 | |
---|
74 | T denominator( Rational(T) r ) { |
---|
75 | return r.denominator; |
---|
76 | } // denominator |
---|
77 | |
---|
78 | [ T, T ] ?=?( & [ T, T ] dest, Rational(T) src ) { |
---|
79 | return dest = src.[ numerator, denominator ]; |
---|
80 | } // ?=? |
---|
81 | |
---|
82 | // setter for numerator/denominator |
---|
83 | |
---|
84 | T numerator( Rational(T) r, T n ) { |
---|
85 | T prev = r.numerator; |
---|
86 | T t = gcd( abs( n ), r.denominator ); // simplify |
---|
87 | r.[numerator, denominator] = [n / t, r.denominator / t]; |
---|
88 | return prev; |
---|
89 | } // numerator |
---|
90 | |
---|
91 | T denominator( Rational(T) r, T d ) { |
---|
92 | T prev = r.denominator; |
---|
93 | T t = simplify( r.numerator, d ); // simplify |
---|
94 | r.[numerator, denominator] = [r.numerator / t, d / t]; |
---|
95 | return prev; |
---|
96 | } // denominator |
---|
97 | |
---|
98 | // comparison |
---|
99 | |
---|
100 | int ?==?( Rational(T) l, Rational(T) r ) { |
---|
101 | return l.numerator * r.denominator == l.denominator * r.numerator; |
---|
102 | } // ?==? |
---|
103 | |
---|
104 | int ?!=?( Rational(T) l, Rational(T) r ) { |
---|
105 | return ! ( l == r ); |
---|
106 | } // ?!=? |
---|
107 | |
---|
108 | int ?!=?( Rational(T) l, zero_t ) { |
---|
109 | return ! ( l == (Rational(T)){ 0 } ); |
---|
110 | } // ?!=? |
---|
111 | |
---|
112 | int ?<?( Rational(T) l, Rational(T) r ) { |
---|
113 | return l.numerator * r.denominator < l.denominator * r.numerator; |
---|
114 | } // ?<? |
---|
115 | |
---|
116 | int ?<=?( Rational(T) l, Rational(T) r ) { |
---|
117 | return l.numerator * r.denominator <= l.denominator * r.numerator; |
---|
118 | } // ?<=? |
---|
119 | |
---|
120 | int ?>?( Rational(T) l, Rational(T) r ) { |
---|
121 | return ! ( l <= r ); |
---|
122 | } // ?>? |
---|
123 | |
---|
124 | int ?>=?( Rational(T) l, Rational(T) r ) { |
---|
125 | return ! ( l < r ); |
---|
126 | } // ?>=? |
---|
127 | |
---|
128 | // arithmetic |
---|
129 | |
---|
130 | Rational(T) +?( Rational(T) r ) { |
---|
131 | return (Rational(T)){ r.numerator, r.denominator }; |
---|
132 | } // +? |
---|
133 | |
---|
134 | Rational(T) -?( Rational(T) r ) { |
---|
135 | return (Rational(T)){ -r.numerator, r.denominator }; |
---|
136 | } // -? |
---|
137 | |
---|
138 | Rational(T) ?+?( Rational(T) l, Rational(T) r ) { |
---|
139 | if ( l.denominator == r.denominator ) { // special case |
---|
140 | return (Rational(T)){ l.numerator + r.numerator, l.denominator }; |
---|
141 | } else { |
---|
142 | return (Rational(T)){ l.numerator * r.denominator + l.denominator * r.numerator, l.denominator * r.denominator }; |
---|
143 | } // if |
---|
144 | } // ?+? |
---|
145 | |
---|
146 | Rational(T) ?+=?( Rational(T) & l, Rational(T) r ) { |
---|
147 | l = l + r; |
---|
148 | return l; |
---|
149 | } // ?+? |
---|
150 | |
---|
151 | Rational(T) ?+=?( Rational(T) & l, one_t ) { |
---|
152 | l = l + (Rational(T)){ 1 }; |
---|
153 | return l; |
---|
154 | } // ?+? |
---|
155 | |
---|
156 | Rational(T) ?-?( Rational(T) l, Rational(T) r ) { |
---|
157 | if ( l.denominator == r.denominator ) { // special case |
---|
158 | return (Rational(T)){ l.numerator - r.numerator, l.denominator }; |
---|
159 | } else { |
---|
160 | return (Rational(T)){ l.numerator * r.denominator - l.denominator * r.numerator, l.denominator * r.denominator }; |
---|
161 | } // if |
---|
162 | } // ?-? |
---|
163 | |
---|
164 | Rational(T) ?-=?( Rational(T) & l, Rational(T) r ) { |
---|
165 | l = l - r; |
---|
166 | return l; |
---|
167 | } // ?-? |
---|
168 | |
---|
169 | Rational(T) ?-=?( Rational(T) & l, one_t ) { |
---|
170 | l = l - (Rational(T)){ 1 }; |
---|
171 | return l; |
---|
172 | } // ?-? |
---|
173 | |
---|
174 | Rational(T) ?*?( Rational(T) l, Rational(T) r ) { |
---|
175 | return (Rational(T)){ l.numerator * r.numerator, l.denominator * r.denominator }; |
---|
176 | } // ?*? |
---|
177 | |
---|
178 | Rational(T) ?*=?( Rational(T) & l, Rational(T) r ) { |
---|
179 | return l = l * r; |
---|
180 | } // ?*? |
---|
181 | |
---|
182 | Rational(T) ?/?( Rational(T) l, Rational(T) r ) { |
---|
183 | if ( r.numerator < (T){0} ) { |
---|
184 | r.[numerator, denominator] = [-r.numerator, -r.denominator]; |
---|
185 | } // if |
---|
186 | return (Rational(T)){ l.numerator * r.denominator, l.denominator * r.numerator }; |
---|
187 | } // ?/? |
---|
188 | |
---|
189 | Rational(T) ?/=?( Rational(T) & l, Rational(T) r ) { |
---|
190 | return l = l / r; |
---|
191 | } // ?/? |
---|
192 | |
---|
193 | // I/O |
---|
194 | |
---|
195 | forall( istype & | istream( istype ) | { istype & ?|?( istype &, T & ); } ) |
---|
196 | istype & ?|?( istype & is, Rational(T) & r ) { |
---|
197 | is | r.numerator | r.denominator; |
---|
198 | T t = simplify( r.numerator, r.denominator ); |
---|
199 | r.numerator /= t; |
---|
200 | r.denominator /= t; |
---|
201 | return is; |
---|
202 | } // ?|? |
---|
203 | |
---|
204 | forall( ostype & | ostream( ostype ) | { ostype & ?|?( ostype &, T ); } ) { |
---|
205 | ostype & ?|?( ostype & os, Rational(T) r ) { |
---|
206 | return os | r.numerator | '/' | r.denominator; |
---|
207 | } // ?|? |
---|
208 | |
---|
209 | void ?|?( ostype & os, Rational(T) r ) { |
---|
210 | (ostype &)(os | r); ends( os ); |
---|
211 | } // ?|? |
---|
212 | } // distribution |
---|
213 | } // distribution |
---|
214 | |
---|
215 | forall( T | Arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { |
---|
216 | Rational(T) ?\?( Rational(T) x, long int y ) { |
---|
217 | if ( y < 0 ) { |
---|
218 | return (Rational(T)){ x.denominator \ -y, x.numerator \ -y }; |
---|
219 | } else { |
---|
220 | return (Rational(T)){ x.numerator \ y, x.denominator \ y }; |
---|
221 | } // if |
---|
222 | } // ?\? |
---|
223 | |
---|
224 | Rational(T) ?\=?( Rational(T) & x, long int y ) { |
---|
225 | return x = x \ y; |
---|
226 | } // ?\? |
---|
227 | } // distribution |
---|
228 | |
---|
229 | // conversion |
---|
230 | |
---|
231 | forall( T | Arithmetic( T ) | { double convert( T ); } ) |
---|
232 | double widen( Rational(T) r ) { |
---|
233 | return convert( r.numerator ) / convert( r.denominator ); |
---|
234 | } // widen |
---|
235 | |
---|
236 | forall( T | Arithmetic( T ) | { double convert( T ); T convert( double ); } ) |
---|
237 | Rational(T) narrow( double f, T md ) { |
---|
238 | // http://www.ics.uci.edu/~eppstein/numth/frap.c |
---|
239 | if ( md <= (T){1} ) { // maximum fractional digits too small? |
---|
240 | return (Rational(T)){ convert( f ), (T){1}}; // truncate fraction |
---|
241 | } // if |
---|
242 | |
---|
243 | // continued fraction coefficients |
---|
244 | T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; |
---|
245 | T ai, t; |
---|
246 | |
---|
247 | // find terms until denom gets too big |
---|
248 | for () { |
---|
249 | ai = convert( f ); |
---|
250 | if ( ! (m10 * ai + m11 <= md) ) break; |
---|
251 | t = m00 * ai + m01; |
---|
252 | m01 = m00; |
---|
253 | m00 = t; |
---|
254 | t = m10 * ai + m11; |
---|
255 | m11 = m10; |
---|
256 | m10 = t; |
---|
257 | double temp = convert( ai ); |
---|
258 | if ( f == temp ) break; // prevent division by zero |
---|
259 | f = 1 / (f - temp); |
---|
260 | if ( f > (double)0x7FFFFFFF ) break; // representation failure |
---|
261 | } // for |
---|
262 | return (Rational(T)){ m00, m10 }; |
---|
263 | } // narrow |
---|
264 | |
---|
265 | // Local Variables: // |
---|
266 | // tab-width: 4 // |
---|
267 | // End: // |
---|