1 // Public rational number operations.
7 #include "cl_rational_class.h"
8 #include "cl_integer_class.h"
11 CL_DEFINE_AS_CONVERSION(cl_RA)
14 // numerator(r) liefert den Zähler der rationalen Zahl r.
15 extern const cl_I numerator (const cl_RA& r);
17 // denominator(r) liefert den Nenner (> 0) der rationalen Zahl r.
18 extern const cl_I denominator (const cl_RA& r);
21 // Liefert (- r), wo r eine rationale Zahl ist.
22 extern const cl_RA operator- (const cl_RA& r);
24 // (+ r s), wo r und s rationale Zahlen sind.
25 extern const cl_RA operator+ (const cl_RA& r, const cl_RA& s);
26 // Dem C++-Compiler muß man auch das Folgende sagen:
27 inline const cl_RA operator+ (const int x, const cl_RA& y)
28 { return cl_I(x) + y; }
29 inline const cl_RA operator+ (const unsigned int x, const cl_RA& y)
30 { return cl_I(x) + y; }
31 inline const cl_RA operator+ (const long x, const cl_RA& y)
32 { return cl_I(x) + y; }
33 inline const cl_RA operator+ (const unsigned long x, const cl_RA& y)
34 { return cl_I(x) + y; }
35 inline const cl_RA operator+ (const cl_RA& x, const int y)
36 { return x + cl_I(y); }
37 inline const cl_RA operator+ (const cl_RA& x, const unsigned int y)
38 { return x + cl_I(y); }
39 inline const cl_RA operator+ (const cl_RA& x, const long y)
40 { return x + cl_I(y); }
41 inline const cl_RA operator+ (const cl_RA& x, const unsigned long y)
42 { return x + cl_I(y); }
44 // (- r s), wo r und s rationale Zahlen sind.
45 extern const cl_RA operator- (const cl_RA& r, const cl_RA& s);
46 // Dem C++-Compiler muß man auch das Folgende sagen:
47 inline const cl_RA operator- (const int x, const cl_RA& y)
48 { return cl_I(x) - y; }
49 inline const cl_RA operator- (const unsigned int x, const cl_RA& y)
50 { return cl_I(x) - y; }
51 inline const cl_RA operator- (const long x, const cl_RA& y)
52 { return cl_I(x) - y; }
53 inline const cl_RA operator- (const unsigned long x, const cl_RA& y)
54 { return cl_I(x) - y; }
55 inline const cl_RA operator- (const cl_RA& x, const int y)
56 { return x - cl_I(y); }
57 inline const cl_RA operator- (const cl_RA& x, const unsigned int y)
58 { return x - cl_I(y); }
59 inline const cl_RA operator- (const cl_RA& x, const long y)
60 { return x - cl_I(y); }
61 inline const cl_RA operator- (const cl_RA& x, const unsigned long y)
62 { return x - cl_I(y); }
64 // (1+ r), wo r eine rationale Zahl ist.
65 extern const cl_RA plus1 (const cl_RA& r);
67 // (1- r), wo r eine rationale Zahl ist.
68 extern const cl_RA minus1 (const cl_RA& r);
70 // (abs r), wo r eine rationale Zahl ist.
71 extern const cl_RA abs (const cl_RA& r);
73 // cl_equal(r,s) vergleicht zwei rationale Zahlen r und s auf Gleichheit.
74 extern cl_boolean cl_equal (const cl_RA& r, const cl_RA& s);
75 // cl_equal_hashcode(r) liefert einen cl_equal-invarianten Hashcode für r.
76 extern uint32 cl_equal_hashcode (const cl_RA& r);
78 // cl_compare(r,s) vergleicht zwei rationale Zahlen r und s.
79 // Ergebnis: 0 falls r=s, +1 falls r>s, -1 falls r<s.
80 extern cl_signean cl_compare (const cl_RA& r, const cl_RA& s);
82 inline bool operator== (const cl_RA& x, const cl_RA& y)
83 { return cl_equal(x,y); }
84 inline bool operator!= (const cl_RA& x, const cl_RA& y)
85 { return !cl_equal(x,y); }
86 inline bool operator<= (const cl_RA& x, const cl_RA& y)
87 { return cl_compare(x,y)<=0; }
88 inline bool operator< (const cl_RA& x, const cl_RA& y)
89 { return cl_compare(x,y)<0; }
90 inline bool operator>= (const cl_RA& x, const cl_RA& y)
91 { return cl_compare(x,y)>=0; }
92 inline bool operator> (const cl_RA& x, const cl_RA& y)
93 { return cl_compare(x,y)>0; }
95 // minusp(x) == (< x 0)
96 extern cl_boolean minusp (const cl_RA& x);
98 // zerop(x) stellt fest, ob eine rationale Zahl = 0 ist.
99 extern cl_boolean zerop (const cl_RA& x);
101 // plusp(x) == (> x 0)
102 extern cl_boolean plusp (const cl_RA& x);
104 // Kehrwert (/ r), wo r eine rationale Zahl ist.
105 extern const cl_RA recip (const cl_RA& r);
107 // Liefert (* r s), wo r und s rationale Zahlen sind.
108 extern const cl_RA operator* (const cl_RA& r, const cl_RA& s);
109 // Dem C++-Compiler muß man auch das Folgende sagen:
110 inline const cl_RA operator* (const int x, const cl_RA& y)
111 { return cl_I(x) * y; }
112 inline const cl_RA operator* (const unsigned int x, const cl_RA& y)
113 { return cl_I(x) * y; }
114 inline const cl_RA operator* (const long x, const cl_RA& y)
115 { return cl_I(x) * y; }
116 inline const cl_RA operator* (const unsigned long x, const cl_RA& y)
117 { return cl_I(x) * y; }
118 inline const cl_RA operator* (const cl_RA& x, const int y)
119 { return x * cl_I(y); }
120 inline const cl_RA operator* (const cl_RA& x, const unsigned int y)
121 { return x * cl_I(y); }
122 inline const cl_RA operator* (const cl_RA& x, const long y)
123 { return x * cl_I(y); }
124 inline const cl_RA operator* (const cl_RA& x, const unsigned long y)
125 { return x * cl_I(y); }
127 // Quadrat (* r r), wo r eine rationale Zahl ist.
128 extern const cl_RA square (const cl_RA& r);
130 // Liefert (/ r s), wo r und s rationale Zahlen sind.
131 extern const cl_RA operator/ (const cl_RA& r, const cl_RA& s);
132 // Dem C++-Compiler muß man auch das Folgende sagen:
133 inline const cl_RA operator/ (const int x, const cl_RA& y)
134 { return cl_I(x) / y; }
135 inline const cl_RA operator/ (const unsigned int x, const cl_RA& y)
136 { return cl_I(x) / y; }
137 inline const cl_RA operator/ (const long x, const cl_RA& y)
138 { return cl_I(x) / y; }
139 inline const cl_RA operator/ (const unsigned long x, const cl_RA& y)
140 { return cl_I(x) / y; }
141 inline const cl_RA operator/ (const cl_RA& x, const int y)
142 { return x / cl_I(y); }
143 inline const cl_RA operator/ (const cl_RA& x, const unsigned int y)
144 { return x / cl_I(y); }
145 inline const cl_RA operator/ (const cl_RA& x, const long y)
146 { return x / cl_I(y); }
147 inline const cl_RA operator/ (const cl_RA& x, const unsigned long y)
148 { return x / cl_I(y); }
150 // Return type for rounding operators.
151 // x / y --> (q,r) with x = y*q+r.
157 cl_RA_div_t (const cl_I& q, const cl_RA& r) : quotient(q), remainder(r) {}
160 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
161 // (q,r) := (floor x)
163 // > x: rationale Zahl
164 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
165 extern const cl_RA_div_t floor2 (const cl_RA& x);
166 extern const cl_I floor1 (const cl_RA& x);
168 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
169 // (q,r) := (ceiling x)
171 // > x: rationale Zahl
172 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
173 extern const cl_RA_div_t ceiling2 (const cl_RA& x);
174 extern const cl_I ceiling1 (const cl_RA& x);
176 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
177 // (q,r) := (truncate x)
179 // > x: rationale Zahl
180 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
181 extern const cl_RA_div_t truncate2 (const cl_RA& x);
182 extern const cl_I truncate1 (const cl_RA& x);
184 // Liefert ganzzahligen und gebrochenen Anteil einer rationalen Zahl.
185 // (q,r) := (round x)
187 // > x: rationale Zahl
188 // < q,r: Quotient q, ein Integer, Rest r, eine rationale Zahl
189 extern const cl_RA_div_t round2 (const cl_RA& x);
190 extern const cl_I round1 (const cl_RA& x);
192 // floor2(x,y) liefert (floor x y).
193 extern const cl_RA_div_t floor2 (const cl_RA& x, const cl_RA& y);
194 extern const cl_I floor1 (const cl_RA& x, const cl_RA& y);
196 // ceiling2(x,y) liefert (ceiling x y).
197 extern const cl_RA_div_t ceiling2 (const cl_RA& x, const cl_RA& y);
198 extern const cl_I ceiling1 (const cl_RA& x, const cl_RA& y);
200 // truncate2(x,y) liefert (truncate x y).
201 extern const cl_RA_div_t truncate2 (const cl_RA& x, const cl_RA& y);
202 extern const cl_I truncate1 (const cl_RA& x, const cl_RA& y);
204 // round2(x,y) liefert (round x y).
205 extern const cl_RA_div_t round2 (const cl_RA& x, const cl_RA& y);
206 extern const cl_I round1 (const cl_RA& x, const cl_RA& y);
208 // max(x,y) liefert (max x y), wo x und y rationale Zahlen sind.
209 extern const cl_RA max (const cl_RA& x, const cl_RA& y);
211 // min(x,y) liefert (min x y), wo x und y rationale Zahlen sind.
212 extern const cl_RA min (const cl_RA& x, const cl_RA& y);
214 // signum(x) liefert (signum x), wo x eine rationale Zahl ist.
215 extern const cl_RA signum (const cl_RA& x);
217 // (expt x y), wo x eine rationale Zahl und y ein Integer >0 ist.
218 extern const cl_RA expt_pos (const cl_RA& x, uintL y);
219 extern const cl_RA expt_pos (const cl_RA& x, const cl_I& y);
221 // (expt x y), wo x eine rationale Zahl und y ein Integer ist.
222 extern const cl_RA expt (const cl_RA& x, sintL y);
223 extern const cl_RA expt (const cl_RA& x, const cl_I& y);
225 // Stellt fest, ob eine rationale Zahl >=0 das Quadrat einer rationalen Zahl
228 // > x: eine rationale Zahl >=0
229 // < w: rationale Zahl (sqrt x) falls x Quadratzahl
230 // < ergebnis: cl_true ..................., cl_false sonst
231 extern cl_boolean sqrtp (const cl_RA& x, cl_RA* w);
233 // Stellt fest, ob eine rationale Zahl >=0 die n-te Potenz einer rationalen Zahl
236 // > x: eine rationale Zahl >=0
237 // > n: ein Integer >0
238 // < w: exakte n-te Wurzel (expt x (/ n)) falls x eine n-te Potenz
239 // < ergebnis: cl_true ........................, cl_false sonst
240 extern cl_boolean rootp (const cl_RA& x, uintL n, cl_RA* w);
241 extern cl_boolean rootp (const cl_RA& x, const cl_I& n, cl_RA* w);
243 // Liefert zu Integers a>0, b>1 den Logarithmus log(a,b),
244 // falls er eine rationale Zahl ist.
246 // > a: ein Integer >0
247 // > b: ein Integer >1
248 // < l: log(a,b) falls er eine exakte rationale Zahl ist
249 // < ergebnis: cl_true ......................................., cl_false sonst
250 extern cl_boolean logp (const cl_I& a, const cl_I& b, cl_RA* l);
252 // Liefert zu rationalen Zahlen a>0, b>0 den Logarithmus log(a,b),
253 // falls er eine rationale Zahl ist.
255 // > a: eine rationale Zahl >0
256 // > b: eine rationale Zahl >0, /=1
257 // < l: log(a,b) falls er eine exakte rationale Zahl ist
258 // < ergebnis: cl_true ......................................., cl_false sonst
259 extern cl_boolean logp (const cl_RA& a, const cl_RA& b, cl_RA* l);
261 // Konversion zu einem C "float".
262 extern float cl_float_approx (const cl_RA& x);
264 // Konversion zu einem C "double".
265 extern double cl_double_approx (const cl_RA& x);
268 #ifdef WANT_OBFUSCATING_OPERATORS
269 // This could be optimized to use in-place operations.
270 inline cl_RA& operator+= (cl_RA& x, const cl_RA& y) { return x = x + y; }
271 inline cl_RA& operator+= (cl_RA& x, const int y) { return x = x + y; }
272 inline cl_RA& operator+= (cl_RA& x, const unsigned int y) { return x = x + y; }
273 inline cl_RA& operator+= (cl_RA& x, const long y) { return x = x + y; }
274 inline cl_RA& operator+= (cl_RA& x, const unsigned long y) { return x = x + y; }
275 inline cl_RA& operator++ /* prefix */ (cl_RA& x) { return x = plus1(x); }
276 inline void operator++ /* postfix */ (cl_RA& x, int dummy) { (void)dummy; x = plus1(x); }
277 inline cl_RA& operator-= (cl_RA& x, const cl_RA& y) { return x = x - y; }
278 inline cl_RA& operator-= (cl_RA& x, const int y) { return x = x - y; }
279 inline cl_RA& operator-= (cl_RA& x, const unsigned int y) { return x = x - y; }
280 inline cl_RA& operator-= (cl_RA& x, const long y) { return x = x - y; }
281 inline cl_RA& operator-= (cl_RA& x, const unsigned long y) { return x = x - y; }
282 inline cl_RA& operator-- /* prefix */ (cl_RA& x) { return x = minus1(x); }
283 inline void operator-- /* postfix */ (cl_RA& x, int dummy) { (void)dummy; x = minus1(x); }
284 inline cl_RA& operator*= (cl_RA& x, const cl_RA& y) { return x = x * y; }
285 inline cl_RA& operator/= (cl_RA& x, const cl_RA& y) { return x = x / y; }
289 // Runtime typing support.
290 extern cl_class cl_class_ratio;
293 // Debugging support.
295 extern int cl_RA_debug_module;
296 static void* const cl_RA_debug_dummy[] = { &cl_RA_debug_dummy,
302 #endif /* _CL_RATIONAL_H */