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