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