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 Oct 6 07:52:13 2023 |
---|
13 | // Update Count : 198 |
---|
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 | } // 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 ] dst, rational(T) src ) { |
---|
79 | return dst = 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 | OSTYPE_VOID_IMPL( rational(T) ) |
---|
209 | } // distribution |
---|
210 | } // distribution |
---|
211 | |
---|
212 | forall( T | arithmetic( T ) | { T ?\?( T, unsigned long ); } ) { |
---|
213 | rational(T) ?\?( rational(T) x, long int y ) { |
---|
214 | if ( y < 0 ) { |
---|
215 | return (rational(T)){ x.denominator \ -y, x.numerator \ -y }; |
---|
216 | } else { |
---|
217 | return (rational(T)){ x.numerator \ y, x.denominator \ y }; |
---|
218 | } // if |
---|
219 | } // ?\? |
---|
220 | |
---|
221 | rational(T) ?\=?( rational(T) & x, long int y ) { |
---|
222 | return x = x \ y; |
---|
223 | } // ?\? |
---|
224 | } // distribution |
---|
225 | |
---|
226 | // conversion |
---|
227 | |
---|
228 | forall( T | arithmetic( T ) | { double convert( T ); } ) |
---|
229 | double widen( rational(T) r ) { |
---|
230 | return convert( r.numerator ) / convert( r.denominator ); |
---|
231 | } // widen |
---|
232 | |
---|
233 | forall( T | arithmetic( T ) | { double convert( T ); T convert( double ); } ) |
---|
234 | rational(T) narrow( double f, T md ) { |
---|
235 | // http://www.ics.uci.edu/~eppstein/numth/frap.c |
---|
236 | if ( md <= (T){1} ) { // maximum fractional digits too small? |
---|
237 | return (rational(T)){ convert( f ), (T){1}}; // truncate fraction |
---|
238 | } // if |
---|
239 | |
---|
240 | // continued fraction coefficients |
---|
241 | T m00 = {1}, m11 = { 1 }, m01 = { 0 }, m10 = { 0 }; |
---|
242 | T ai, t; |
---|
243 | |
---|
244 | // find terms until denom gets too big |
---|
245 | for () { |
---|
246 | ai = convert( f ); |
---|
247 | if ( ! (m10 * ai + m11 <= md) ) break; |
---|
248 | t = m00 * ai + m01; |
---|
249 | m01 = m00; |
---|
250 | m00 = t; |
---|
251 | t = m10 * ai + m11; |
---|
252 | m11 = m10; |
---|
253 | m10 = t; |
---|
254 | double temp = convert( ai ); |
---|
255 | if ( f == temp ) break; // prevent division by zero |
---|
256 | f = 1 / (f - temp); |
---|
257 | if ( f > (double)0x7FFFFFFF ) break; // representation failure |
---|
258 | } // for |
---|
259 | return (rational(T)){ m00, m10 }; |
---|
260 | } // narrow |
---|
261 | |
---|
262 | // Local Variables: // |
---|
263 | // tab-width: 4 // |
---|
264 | // End: // |
---|