]> www.ginac.de Git - cln.git/blob - include/cln/rational.h
Ensure that makeinfo ≥ 6.8 checks the @menu structure.
[cln.git] / include / cln / rational.h
1 // Public rational number operations.
2
3 #ifndef _CL_RATIONAL_H
4 #define _CL_RATIONAL_H
5
6 #include "cln/number.h"
7 #include "cln/rational_class.h"
8 #include "cln/integer_class.h"
9 #include "cln/exception.h"
10
11 namespace cln {
12
13 CL_DEFINE_AS_CONVERSION(cl_RA)
14
15
16 // numerator(r) liefert den Zähler der rationalen Zahl r.
17 extern const cl_I numerator (const cl_RA& r);
18
19 // denominator(r) liefert den Nenner (> 0) der rationalen Zahl r.
20 extern const cl_I denominator (const cl_RA& r);
21
22
23 // Liefert (- r), wo r eine rationale Zahl ist.
24 extern const cl_RA operator- (const cl_RA& r);
25
26 // (+ r s), wo r und s rationale Zahlen sind.
27 extern const cl_RA operator+ (const cl_RA& r, const cl_RA& s);
28 // Dem C++-Compiler muß man auch das Folgende sagen:
29 inline const cl_RA operator+ (const int x, const cl_RA& y)
30         { return cl_I(x) + y; }
31 inline const cl_RA operator+ (const unsigned int x, const cl_RA& y)
32         { return cl_I(x) + y; }
33 inline const cl_RA operator+ (const long x, const cl_RA& y)
34         { return cl_I(x) + y; }
35 inline const cl_RA operator+ (const unsigned long x, const cl_RA& y)
36         { return cl_I(x) + y; }
37 inline const cl_RA operator+ (const long long x, const cl_RA& y)
38         { return cl_I(x) + y; }
39 inline const cl_RA operator+ (const unsigned long long x, const cl_RA& y)
40         { return cl_I(x) + y; }
41 inline const cl_RA operator+ (const cl_RA& x, const int y)
42         { return x + cl_I(y); }
43 inline const cl_RA operator+ (const cl_RA& x, const unsigned int y)
44         { return x + cl_I(y); }
45 inline const cl_RA operator+ (const cl_RA& x, const long y)
46         { return x + cl_I(y); }
47 inline const cl_RA operator+ (const cl_RA& x, const unsigned long y)
48         { return x + cl_I(y); }
49 inline const cl_RA operator+ (const cl_RA& x, const long long y)
50         { return x + cl_I(y); }
51 inline const cl_RA operator+ (const cl_RA& x, const unsigned long long y)
52         { return x + cl_I(y); }
53
54 // (- r s), wo r und s rationale Zahlen sind.
55 extern const cl_RA operator- (const cl_RA& r, const cl_RA& s);
56 // Dem C++-Compiler muß man auch das Folgende sagen:
57 inline const cl_RA operator- (const int x, const cl_RA& y)
58         { return cl_I(x) - y; }
59 inline const cl_RA operator- (const unsigned int x, const cl_RA& y)
60         { return cl_I(x) - y; }
61 inline const cl_RA operator- (const long x, const cl_RA& y)
62         { return cl_I(x) - y; }
63 inline const cl_RA operator- (const unsigned long x, const cl_RA& y)
64         { return cl_I(x) - y; }
65 inline const cl_RA operator- (const long long x, const cl_RA& y)
66         { return cl_I(x) - y; }
67 inline const cl_RA operator- (const unsigned long long x, const cl_RA& y)
68         { return cl_I(x) - y; }
69 inline const cl_RA operator- (const cl_RA& x, const int y)
70         { return x - cl_I(y); }
71 inline const cl_RA operator- (const cl_RA& x, const unsigned int y)
72         { return x - cl_I(y); }
73 inline const cl_RA operator- (const cl_RA& x, const long y)
74         { return x - cl_I(y); }
75 inline const cl_RA operator- (const cl_RA& x, const unsigned long y)
76         { return x - cl_I(y); }
77 inline const cl_RA operator- (const cl_RA& x, const long long y)
78         { return x - cl_I(y); }
79 inline const cl_RA operator- (const cl_RA& x, const unsigned long long y)
80         { return x - cl_I(y); }
81
82 // (1+ r), wo r eine rationale Zahl ist.
83 extern const cl_RA plus1 (const cl_RA& r);
84
85 // (1- r), wo r eine rationale Zahl ist.
86 extern const cl_RA minus1 (const cl_RA& r);
87
88 // (abs r), wo r eine rationale Zahl ist.
89 extern const cl_RA abs (const cl_RA& r);
90
91 // equal(r,s) vergleicht zwei rationale Zahlen r und s auf Gleichheit.
92 extern bool equal (const cl_RA& r, const cl_RA& s);
93 // equal_hashcode(r) liefert einen equal-invarianten Hashcode für r.
94 extern uint32 equal_hashcode (const cl_RA& r);
95
96 // compare(r,s) vergleicht zwei rationale Zahlen r und s.
97 // Ergebnis: 0 falls r=s, +1 falls r>s, -1 falls r<s.
98 extern cl_signean compare (const cl_RA& r, const cl_RA& s);
99
100 inline bool operator== (const cl_RA& x, const cl_RA& y)
101         { return equal(x,y); }
102 inline bool operator!= (const cl_RA& x, const cl_RA& y)
103         { return !equal(x,y); }
104 inline bool operator<= (const cl_RA& x, const cl_RA& y)
105         { return compare(x,y)<=0; }
106 inline bool operator< (const cl_RA& x, const cl_RA& y)
107         { return compare(x,y)<0; }
108 inline bool operator>= (const cl_RA& x, const cl_RA& y)
109         { return compare(x,y)>=0; }
110 inline bool operator> (const cl_RA& x, const cl_RA& y)
111         { return compare(x,y)>0; }
112
113 // minusp(x) == (< x 0)
114 extern bool minusp (const cl_RA& x);
115
116 // zerop(x) stellt fest, ob eine rationale Zahl = 0 ist.
117 extern bool zerop (const cl_RA& x);
118
119 // plusp(x) == (> x 0)
120 extern bool plusp (const cl_RA& x);
121
122 // Kehrwert (/ r), wo r eine rationale Zahl ist.
123 extern const cl_RA recip (const cl_RA& r);
124
125 // Liefert (* r s), wo r und s rationale Zahlen sind.
126 extern const cl_RA operator* (const cl_RA& r, const cl_RA& s);
127 // Dem C++-Compiler muß man auch das Folgende sagen:
128 inline const cl_RA operator* (const int x, const cl_RA& y)
129         { return cl_I(x) * y; }
130 inline const cl_RA operator* (const unsigned int x, const cl_RA& y)
131         { return cl_I(x) * y; }
132 inline const cl_RA operator* (const long x, const cl_RA& y)
133         { return cl_I(x) * y; }
134 inline const cl_RA operator* (const unsigned long x, const cl_RA& y)
135         { return cl_I(x) * y; }
136 inline const cl_RA operator* (const long long x, const cl_RA& y)
137         { return cl_I(x) * y; }
138 inline const cl_RA operator* (const unsigned long long x, const cl_RA& y)
139         { return cl_I(x) * y; }
140 inline const cl_RA operator* (const cl_RA& x, const int y)
141         { return x * cl_I(y); }
142 inline const cl_RA operator* (const cl_RA& x, const unsigned int y)
143         { return x * cl_I(y); }
144 inline const cl_RA operator* (const cl_RA& x, const long y)
145         { return x * cl_I(y); }
146 inline const cl_RA operator* (const cl_RA& x, const unsigned long y)
147         { return x * cl_I(y); }
148 inline const cl_RA operator* (const cl_RA& x, const long long y)
149         { return x * cl_I(y); }
150 inline const cl_RA operator* (const cl_RA& x, const unsigned long long y)
151         { return x * cl_I(y); }
152
153 // Quadrat (* r r), wo r eine rationale Zahl ist.
154 extern const cl_RA square (const cl_RA& r);
155
156 // Liefert (/ r s), wo r und s rationale Zahlen sind.
157 extern const cl_RA operator/ (const cl_RA& r, const cl_RA& s);
158 // Dem C++-Compiler muß man auch das Folgende sagen:
159 inline const cl_RA operator/ (const int x, const cl_RA& y)
160         { return cl_I(x) / y; }
161 inline const cl_RA operator/ (const unsigned int x, const cl_RA& y)
162         { return cl_I(x) / y; }
163 inline const cl_RA operator/ (const long x, const cl_RA& y)
164         { return cl_I(x) / y; }
165 inline const cl_RA operator/ (const unsigned long x, const cl_RA& y)
166         { return cl_I(x) / y; }
167 inline const cl_RA operator/ (const long long x, const cl_RA& y)
168         { return cl_I(x) / y; }
169 inline const cl_RA operator/ (const unsigned long long x, const cl_RA& y)
170         { return cl_I(x) / y; }
171 inline const cl_RA operator/ (const cl_RA& x, const int y)
172         { return x / cl_I(y); }
173 inline const cl_RA operator/ (const cl_RA& x, const unsigned int y)
174         { return x / cl_I(y); }
175 inline const cl_RA operator/ (const cl_RA& x, const long y)
176         { return x / cl_I(y); }
177 inline const cl_RA operator/ (const cl_RA& x, const unsigned long y)
178         { return x / cl_I(y); }
179 inline const cl_RA operator/ (const cl_RA& x, const long long y)
180         { return x / cl_I(y); }
181 inline const cl_RA operator/ (const cl_RA& x, const unsigned long long y)
182         { return x / cl_I(y); }
183
184 // Return type for rounding operators.
185 // x / y  --> (q,r) with x = y*q+r.
186 struct cl_RA_div_t {
187         cl_I quotient;
188         cl_RA remainder;
189 // Constructor.
190         cl_RA_div_t () {}
191         cl_RA_div_t (const cl_I& q, const cl_RA& r) : quotient(q), remainder(r) {}
192 };
193
194 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
195 // (q,r) := (floor x)
196 // floor2(x)
197 // > x: rationale Zahl
198 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
199   extern const cl_RA_div_t floor2 (const cl_RA& x);
200   extern const cl_I floor1 (const cl_RA& x);
201
202 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
203 // (q,r) := (ceiling x)
204 // ceiling2(x)
205 // > x: rationale Zahl
206 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
207   extern const cl_RA_div_t ceiling2 (const cl_RA& x);
208   extern const cl_I ceiling1 (const cl_RA& x);
209
210 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
211 // (q,r) := (truncate x)
212 // truncate2(x)
213 // > x: rationale Zahl
214 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
215   extern const cl_RA_div_t truncate2 (const cl_RA& x);
216   extern const cl_I truncate1 (const cl_RA& x);
217
218 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
219 // (q,r) := (round x)
220 // round2(x)
221 // > x: rationale Zahl
222 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
223   extern const cl_RA_div_t round2 (const cl_RA& x);
224   extern const cl_I round1 (const cl_RA& x);
225
226 // floor2(x,y) liefert (floor x y).
227 extern const cl_RA_div_t floor2 (const cl_RA& x, const cl_RA& y);
228 extern const cl_I floor1 (const cl_RA& x, const cl_RA& y);
229
230 // ceiling2(x,y) liefert (ceiling x y).
231 extern const cl_RA_div_t ceiling2 (const cl_RA& x, const cl_RA& y);
232 extern const cl_I ceiling1 (const cl_RA& x, const cl_RA& y);
233
234 // truncate2(x,y) liefert (truncate x y).
235 extern const cl_RA_div_t truncate2 (const cl_RA& x, const cl_RA& y);
236 extern const cl_I truncate1 (const cl_RA& x, const cl_RA& y);
237
238 // round2(x,y) liefert (round x y).
239 extern const cl_RA_div_t round2 (const cl_RA& x, const cl_RA& y);
240 extern const cl_I round1 (const cl_RA& x, const cl_RA& y);
241
242 // max(x,y) liefert (max x y), wo x und y rationale Zahlen sind.
243 extern const cl_RA max (const cl_RA& x, const cl_RA& y);
244
245 // min(x,y) liefert (min x y), wo x und y rationale Zahlen sind.
246 extern const cl_RA min (const cl_RA& x, const cl_RA& y);
247
248 // signum(x) liefert (signum x), wo x eine rationale Zahl ist.
249 extern const cl_RA signum (const cl_RA& x);
250
251 // (expt x y), wo x eine rationale Zahl und y ein Integer >0 ist.
252 extern const cl_RA expt_pos (const cl_RA& x, uintL y);
253 extern const cl_RA expt_pos (const cl_RA& x, const cl_I& y);
254
255 // (expt x y), wo x eine rationale Zahl und y ein Integer ist.
256 extern const cl_RA expt (const cl_RA& x, sintL y);
257 extern const cl_RA expt (const cl_RA& x, const cl_I& y);
258
259 // Stellt fest, ob eine rationale Zahl >=0 das Quadrat einer rationalen Zahl
260 // ist.
261 // sqrtp(x,&w)
262 // > x: eine rationale Zahl >=0
263 // < w: rationale Zahl (sqrt x) falls x Quadratzahl
264 // < ergebnis: true      ..................., false sonst
265   extern bool sqrtp (const cl_RA& x, cl_RA* w);
266
267 // Stellt fest, ob eine rationale Zahl >=0 die n-te Potenz einer rationalen Zahl
268 // ist.
269 // rootp(x,n,&w)
270 // > x: eine rationale Zahl >=0
271 // > n: ein Integer >0
272 // < w: exakte n-te Wurzel (expt x (/ n)) falls x eine n-te Potenz
273 // < ergebnis: true                       ........................, false sonst
274   extern bool rootp (const cl_RA& x, uintL n, cl_RA* w);
275   extern bool rootp (const cl_RA& x, const cl_I& n, cl_RA* w);
276
277 // Liefert zu Integers a>0, b>1 den Logarithmus log(a,b),
278 // falls er eine rationale Zahl ist.
279 // logp(a,b,&l)
280 // > a: ein Integer >0
281 // > b: ein Integer >1
282 // < l: log(a,b)       falls er eine exakte rationale Zahl ist
283 // < ergebnis: true    ......................................., false sonst
284   extern bool logp (const cl_I& a, const cl_I& b, cl_RA* l);
285
286 // Liefert zu rationalen Zahlen a>0, b>0 den Logarithmus log(a,b),
287 // falls er eine rationale Zahl ist.
288 // logp(a,b,&l)
289 // > a: eine rationale Zahl >0
290 // > b: eine rationale Zahl >0, /=1
291 // < l: log(a,b)       falls er eine exakte rationale Zahl ist
292 // < ergebnis: true    ......................................., false sonst
293   extern bool logp (const cl_RA& a, const cl_RA& b, cl_RA* l);
294
295 // Konversion zu einem C "float".
296 extern float float_approx (const cl_RA& x);
297
298 // Konversion zu einem C "double".
299 extern double double_approx (const cl_RA& x);
300
301
302 // This could be optimized to use in-place operations.
303 inline cl_RA& operator+= (cl_RA& x, const cl_RA& y) { return x = x + y; }
304 inline cl_RA& operator+= (cl_RA& x, const int y) { return x = x + y; }
305 inline cl_RA& operator+= (cl_RA& x, const unsigned int y) { return x = x + y; }
306 inline cl_RA& operator+= (cl_RA& x, const long y) { return x = x + y; }
307 inline cl_RA& operator+= (cl_RA& x, const unsigned long y) { return x = x + y; }
308 inline cl_RA& operator+= (cl_RA& x, const long long y) { return x = x + y; }
309 inline cl_RA& operator+= (cl_RA& x, const unsigned long long y) { return x = x + y; }
310 inline cl_RA& operator++ /* prefix */ (cl_RA& x) { return x = plus1(x); }
311 inline void operator++ /* postfix */ (cl_RA& x, int dummy) { (void)dummy; x = plus1(x); }
312 inline cl_RA& operator-= (cl_RA& x, const cl_RA& y) { return x = x - y; }
313 inline cl_RA& operator-= (cl_RA& x, const int y) { return x = x - y; }
314 inline cl_RA& operator-= (cl_RA& x, const unsigned int y) { return x = x - y; }
315 inline cl_RA& operator-= (cl_RA& x, const long y) { return x = x - y; }
316 inline cl_RA& operator-= (cl_RA& x, const unsigned long y) { return x = x - y; }
317 inline cl_RA& operator-= (cl_RA& x, const long long y) { return x = x - y; }
318 inline cl_RA& operator-= (cl_RA& x, const unsigned long long y) { return x = x - y; }
319 inline cl_RA& operator-- /* prefix */ (cl_RA& x) { return x = minus1(x); }
320 inline void operator-- /* postfix */ (cl_RA& x, int dummy) { (void)dummy; x = minus1(x); }
321 inline cl_RA& operator*= (cl_RA& x, const cl_RA& y) { return x = x * y; }
322 inline cl_RA& operator/= (cl_RA& x, const cl_RA& y) { return x = x / y; }
323
324
325 // Runtime typing support.
326 extern cl_class cl_class_ratio;
327
328
329 // Debugging support.
330 #ifdef CL_DEBUG
331 extern int cl_RA_debug_module;
332 CL_FORCE_LINK(cl_RA_debug_dummy, cl_RA_debug_module)
333 #endif
334
335 }  // namespace cln
336
337 #endif /* _CL_RATIONAL_H */