]> www.ginac.de Git - cln.git/blob - include/cln/lfloat.h
2006-04-25 Bruno Haible <bruno@clisp.org>
[cln.git] / include / cln / lfloat.h
1 // Public long float operations.
2
3 #ifndef _CL_LFLOAT_H
4 #define _CL_LFLOAT_H
5
6 #include "cln/number.h"
7 #include "cln/lfloat_class.h"
8 #include "cln/integer_class.h"
9 #include "cln/float.h"
10
11 namespace cln {
12
13 CL_DEFINE_AS_CONVERSION(cl_LF)
14
15
16 // Liefert zu einem Long-Float x : (- x), ein LF.
17 extern const cl_LF operator- (const cl_LF& x);
18
19 // compare(x,y) vergleicht zwei Long-Floats x und y.
20 // Ergebnis: 0 falls x=y, +1 falls x>y, -1 falls x<y.
21 extern cl_signean compare (const cl_LF& x, const cl_LF& y);
22
23 // equal_hashcode(x) liefert einen equal-invarianten Hashcode für x.
24 extern uint32 equal_hashcode (const cl_LF& x);
25
26 inline bool operator== (const cl_LF& x, const cl_LF& y)
27         { return compare(x,y)==0; }
28 inline bool operator!= (const cl_LF& x, const cl_LF& y)
29         { return compare(x,y)!=0; }
30 inline bool operator<= (const cl_LF& x, const cl_LF& y)
31         { return compare(x,y)<=0; }
32 inline bool operator< (const cl_LF& x, const cl_LF& y)
33         { return compare(x,y)<0; }
34 inline bool operator>= (const cl_LF& x, const cl_LF& y)
35         { return compare(x,y)>=0; }
36 inline bool operator> (const cl_LF& x, const cl_LF& y)
37         { return compare(x,y)>0; }
38
39 // minusp(x) == (< x 0)
40 extern cl_boolean minusp (const cl_LF& x);
41
42 // zerop(x) stellt fest, ob ein Long-Float x = 0.0 ist.
43 extern cl_boolean zerop (const cl_LF& x);
44
45 // plusp(x) == (> x 0)
46 extern cl_boolean plusp (const cl_LF& x);
47
48 // Liefert zu zwei Long-Float x und y : (+ x y), ein LF.
49 extern const cl_LF operator+ (const cl_LF& x, const cl_LF& y);
50
51 // Liefert zu zwei Long-Float x und y : (- x y), ein LF.
52 extern const cl_LF operator- (const cl_LF& x, const cl_LF& y);
53
54 // Liefert zu zwei Long-Float x und y : (* x y), ein LF.
55 extern const cl_LF operator* (const cl_LF& x, const cl_LF& y);
56 // Spezialfall x oder y Integer oder rationale Zahl.
57 inline const cl_R operator* (const cl_LF& x, const cl_I& y)
58 {
59         extern const cl_R cl_LF_I_mul (const cl_LF&, const cl_I&);
60         return cl_LF_I_mul(x,y);
61 }
62 inline const cl_R operator* (const cl_I& x, const cl_LF& y)
63 {
64         extern const cl_R cl_LF_I_mul (const cl_LF&, const cl_I&);
65         return cl_LF_I_mul(y,x);
66 }
67 inline const cl_R operator* (const cl_LF& x, const cl_RA& y)
68 {
69         extern const cl_R cl_LF_RA_mul (const cl_LF&, const cl_RA&);
70         return cl_LF_RA_mul(x,y);
71 }
72 inline const cl_R operator* (const cl_RA& x, const cl_LF& y)
73 {
74         extern const cl_R cl_LF_RA_mul (const cl_LF&, const cl_RA&);
75         return cl_LF_RA_mul(y,x);
76 }
77 // Dem C++-Compiler muß man auch das Folgende sagen (wg. `int * cl_LF' u.ä.):
78 inline const cl_R operator* (const int x, const cl_LF& y)
79         { return cl_I(x) * y; }
80 inline const cl_R operator* (const unsigned int x, const cl_LF& y)
81         { return cl_I(x) * y; }
82 inline const cl_R operator* (const long x, const cl_LF& y)
83         { return cl_I(x) * y; }
84 inline const cl_R operator* (const unsigned long x, const cl_LF& y)
85         { return cl_I(x) * y; }
86 inline const cl_R operator* (const cl_LF& x, const int y)
87         { return x * cl_I(y); }
88 inline const cl_R operator* (const cl_LF& x, const unsigned int y)
89         { return x * cl_I(y); }
90 inline const cl_R operator* (const cl_LF& x, const long y)
91         { return x * cl_I(y); }
92 inline const cl_R operator* (const cl_LF& x, const unsigned long y)
93         { return x * cl_I(y); }
94 // Spezialfall x = y.
95 // Liefert zu einem Long-Float x : (* x x), ein LF.
96 extern const cl_LF square (const cl_LF& x);
97
98 // Liefert zu zwei Long-Float x und y : (/ x y), ein LF.
99 extern const cl_LF operator/ (const cl_LF& x, const cl_LF& y);
100 // Spezialfall x oder y Integer oder rationale Zahl.
101 inline const cl_LF operator/ (const cl_LF& x, const cl_I& y)
102 {
103         extern const cl_LF cl_LF_I_div (const cl_LF& x, const cl_I& y);
104         return cl_LF_I_div(x,y);
105 }
106 inline const cl_R operator/ (const cl_I& x, const cl_LF& y)
107 {
108         extern const cl_R cl_I_LF_div (const cl_I& x, const cl_LF& y);
109         return cl_I_LF_div(x,y);
110 }
111 inline const cl_LF operator/ (const cl_LF& x, const cl_RA& y)
112 {
113         extern const cl_LF cl_LF_RA_div (const cl_LF& x, const cl_RA& y);
114         return cl_LF_RA_div(x,y);
115 }
116 inline const cl_R operator/ (const cl_RA& x, const cl_LF& y)
117 {
118         extern const cl_R cl_RA_LF_div (const cl_RA& x, const cl_LF& y);
119         return cl_RA_LF_div(x,y);
120 }
121 // Dem C++-Compiler muß man nun auch das Folgende sagen:
122 inline const cl_LF operator/ (const cl_LF& x, const int y)
123         { return x / cl_I(y); }
124 inline const cl_LF operator/ (const cl_LF& x, const unsigned int y)
125         { return x / cl_I(y); }
126 inline const cl_LF operator/ (const cl_LF& x, const long y)
127         { return x / cl_I(y); }
128 inline const cl_LF operator/ (const cl_LF& x, const unsigned long y)
129         { return x / cl_I(y); }
130 inline const cl_R operator/ (const int x, const cl_LF& y)
131         { return cl_I(x) / y; }
132 inline const cl_R operator/ (const unsigned int x, const cl_LF& y)
133         { return cl_I(x) / y; }
134 inline const cl_R operator/ (const long x, const cl_LF& y)
135         { return cl_I(x) / y; }
136 inline const cl_R operator/ (const unsigned long x, const cl_LF& y)
137         { return cl_I(x) / y; }
138
139 // Liefert zu einem Long-Float x>=0 : (sqrt x), ein LF.
140 extern const cl_LF sqrt (const cl_LF& x);
141
142 // recip(x) liefert (/ x), wo x ein Long-Float ist.
143 extern const cl_LF recip (const cl_LF& x);
144
145 // abs(x) liefert (abs x), wo x ein Long-Float ist.
146 extern const cl_LF abs (const cl_LF& x);
147
148
149 // (1+ x), wo x ein Long-Float ist.
150 extern const cl_LF plus1 (const cl_LF& x);
151
152 // (1- x), wo x ein Long-Float ist.
153 extern const cl_LF minus1 (const cl_LF& x);
154
155
156 // ffloor(x) liefert (ffloor x), wo x ein LF ist.
157 extern const cl_LF ffloor (const cl_LF& x);
158
159 // fceiling(x) liefert (fceiling x), wo x ein LF ist.
160 extern const cl_LF fceiling (const cl_LF& x);
161
162 // ftruncate(x) liefert (ftruncate x), wo x ein LF ist.
163 extern const cl_LF ftruncate (const cl_LF& x);
164
165 // fround(x) liefert (fround x), wo x ein LF ist.
166 extern const cl_LF fround (const cl_LF& x);
167
168
169 // Return type for frounding operators.
170 // x / y  --> (q,r) with x = y*q+r.
171 struct cl_LF_fdiv_t {
172         cl_LF quotient;
173         cl_LF remainder;
174 // Constructor.
175         cl_LF_fdiv_t () {}
176         cl_LF_fdiv_t (const cl_LF& q, const cl_LF& r) : quotient(q), remainder(r) {}
177 };
178
179 // ffloor2(x) liefert (ffloor x), wo x ein LF ist.
180 inline const cl_LF_fdiv_t ffloor2 (const cl_LF& x)
181 {
182         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
183         cl_LF q = ffloor(x);
184         return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
185 }
186
187 // fceiling2(x) liefert (fceiling x), wo x ein LF ist.
188 inline const cl_LF_fdiv_t fceiling2 (const cl_LF& x)
189 {
190         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
191         cl_LF q = fceiling(x);
192         return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
193 }
194
195 // ftruncate2(x) liefert (ftruncate x), wo x ein LF ist.
196 inline const cl_LF_fdiv_t ftruncate2 (const cl_LF& x)
197 {
198         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
199         cl_LF q = ftruncate(x);
200         return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
201 }
202
203 // fround2(x) liefert (fround x), wo x ein LF ist.
204 inline const cl_LF_fdiv_t fround2 (const cl_LF& x)
205 {
206         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
207         cl_LF q = fround(x);
208         return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
209 }
210
211
212 // Return type for rounding operators.
213 // x / y  --> (q,r) with x = y*q+r.
214 struct cl_LF_div_t {
215         cl_I quotient;
216         cl_LF remainder;
217 // Constructor.
218         cl_LF_div_t () {}
219         cl_LF_div_t (const cl_I& q, const cl_LF& r) : quotient(q), remainder(r) {}
220 };
221
222 // floor2(x) liefert (floor x), wo x ein LF ist.
223 inline const cl_LF_div_t floor2 (const cl_LF& x)
224 {
225         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
226         extern const cl_I cl_LF_to_I (const cl_LF& x);
227         cl_LF q = ffloor(x);
228         return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
229 }
230 inline const cl_I floor1 (const cl_LF& x)
231 {
232         extern const cl_I cl_LF_to_I (const cl_LF& x);
233         return cl_LF_to_I(ffloor(x));
234 }
235
236 // ceiling2(x) liefert (ceiling x), wo x ein LF ist.
237 inline const cl_LF_div_t ceiling2 (const cl_LF& x)
238 {
239         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
240         extern const cl_I cl_LF_to_I (const cl_LF& x);
241         cl_LF q = fceiling(x);
242         return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
243 }
244 inline const cl_I ceiling1 (const cl_LF& x)
245 {
246         extern const cl_I cl_LF_to_I (const cl_LF& x);
247         return cl_LF_to_I(fceiling(x));
248 }
249
250 // truncate2(x) liefert (truncate x), wo x ein LF ist.
251 inline const cl_LF_div_t truncate2 (const cl_LF& x)
252 {
253         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
254         extern const cl_I cl_LF_to_I (const cl_LF& x);
255         cl_LF q = ftruncate(x);
256         return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
257 }
258 inline const cl_I truncate1 (const cl_LF& x)
259 {
260         extern const cl_I cl_LF_to_I (const cl_LF& x);
261         return cl_LF_to_I(ftruncate(x));
262 }
263
264 // round2(x) liefert (round x), wo x ein LF ist.
265 inline const cl_LF_div_t round2 (const cl_LF& x)
266 {
267         extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
268         extern const cl_I cl_LF_to_I (const cl_LF& x);
269         cl_LF q = fround(x);
270         return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
271 }
272 inline const cl_I round1 (const cl_LF& x)
273 {
274         extern const cl_I cl_LF_to_I (const cl_LF& x);
275         return cl_LF_to_I(fround(x));
276 }
277
278 // floor2(x,y) liefert (floor x y).
279 extern const cl_LF_div_t floor2 (const cl_LF& x, const cl_LF& y);
280 inline const cl_I floor1 (const cl_LF& x, const cl_LF& y) { return floor1(x/y); }
281
282 // ceiling2(x,y) liefert (ceiling x y).
283 extern const cl_LF_div_t ceiling2 (const cl_LF& x, const cl_LF& y);
284 inline const cl_I ceiling1 (const cl_LF& x, const cl_LF& y) { return ceiling1(x/y); }
285
286 // truncate2(x,y) liefert (truncate x y).
287 extern const cl_LF_div_t truncate2 (const cl_LF& x, const cl_LF& y);
288 inline const cl_I truncate1 (const cl_LF& x, const cl_LF& y) { return truncate1(x/y); }
289
290 // round2(x,y) liefert (round x y).
291 extern const cl_LF_div_t round2 (const cl_LF& x, const cl_LF& y);
292 inline const cl_I round1 (const cl_LF& x, const cl_LF& y) { return round1(x/y); }
293
294
295 // cl_float(x,y) returns a long float if y is a long float.
296 inline const cl_LF cl_float (const cl_F& x, const cl_LF& y)
297 {
298         extern const cl_F cl_float (const cl_F& x, const cl_F& y);
299         return The(cl_LF)(cl_float(x,(const cl_F&)y));
300 }
301 inline const cl_LF cl_float (const cl_I& x, const cl_LF& y)
302 {
303         extern const cl_F cl_float (const cl_I& x, const cl_F& y);
304         return The(cl_LF)(cl_float(x,(const cl_F&)y));
305 }
306 inline const cl_LF cl_float (const cl_RA& x, const cl_LF& y)
307 {
308         extern const cl_F cl_float (const cl_RA& x, const cl_F& y);
309         return The(cl_LF)(cl_float(x,(const cl_F&)y));
310 }
311 inline const cl_LF cl_float (int x, const cl_LF& y)
312         { return cl_float(cl_I(x),y); }
313 inline const cl_LF cl_float (unsigned int x, const cl_LF& y)
314         { return cl_float(cl_I(x),y); }
315
316
317 // Return type for decode_float:
318 struct decoded_lfloat {
319         cl_LF mantissa;
320         cl_I exponent;
321         cl_LF sign;
322 // Constructor.
323         decoded_lfloat () {}
324         decoded_lfloat (const cl_LF& m, const cl_I& e, const cl_LF& s) : mantissa(m), exponent(e), sign(s) {}
325 };
326
327 // decode_float(x) liefert zu einem Float x: (decode-float x).
328 // x = 0.0 liefert (0.0, 0, 1.0).
329 // x = (-1)^s * 2^e * m liefert ((-1)^0 * 2^0 * m, e als Integer, (-1)^s).
330 extern const decoded_lfloat decode_float (const cl_LF& x);
331
332 // float_exponent(x) liefert zu einem Float x:
333 // den Exponenten von (decode-float x).
334 // x = 0.0 liefert 0.
335 // x = (-1)^s * 2^e * m liefert e.
336 extern sintL float_exponent (const cl_LF& x);
337
338 // float_radix(x) liefert (float-radix x), wo x ein Float ist.
339 inline sintL float_radix (const cl_LF& x)
340 {
341         (void)x; // unused x
342         return 2;
343 }
344
345 // float_sign(x) liefert (float-sign x), wo x ein Float ist.
346 extern const cl_LF float_sign (const cl_LF& x);
347
348 // float_digits(x) liefert (float-digits x), wo x ein Float ist.
349 // < ergebnis: ein uintC >0
350 extern uintC float_digits (const cl_LF& x);
351
352 // float_precision(x) liefert (float-precision x), wo x ein Float ist.
353 // < ergebnis: ein uintC >=0
354 extern uintC float_precision (const cl_LF& x);
355
356
357 // integer_decode_float(x) liefert zu einem Float x: (integer-decode-float x).
358 // x = 0.0 liefert (0, 0, 1).
359 // x = (-1)^s * 2^e * m bei Float-Precision p liefert
360 //   (Mantisse 2^p * m als Integer, e-p als Integer, (-1)^s als Fixnum).
361 extern const cl_idecoded_float integer_decode_float (const cl_LF& x);
362
363
364 // scale_float(x,delta) liefert x*2^delta, wo x ein LF ist.
365 extern const cl_LF scale_float (const cl_LF& x, sintC delta);
366 extern const cl_LF scale_float (const cl_LF& x, const cl_I& delta);
367
368
369 // max(x,y) liefert (max x y), wo x und y Floats sind.
370 extern const cl_LF max (const cl_LF& x, const cl_LF& y);
371
372 // min(x,y) liefert (min x y), wo x und y Floats sind.
373 extern const cl_LF min (const cl_LF& x, const cl_LF& y);
374
375 // signum(x) liefert (signum x), wo x ein Float ist.
376 extern const cl_LF signum (const cl_LF& x);
377
378
379 // Konversion zu einem C "float".
380 extern float float_approx (const cl_LF& x);
381
382 // Konversion zu einem C "double".
383 extern double double_approx (const cl_LF& x);
384
385
386 #ifdef WANT_OBFUSCATING_OPERATORS
387 // This could be optimized to use in-place operations.
388 inline cl_LF& operator+= (cl_LF& x, const cl_LF& y) { return x = x + y; }
389 inline cl_LF& operator++ /* prefix */ (cl_LF& x) { return x = plus1(x); }
390 inline void operator++ /* postfix */ (cl_LF& x, int dummy) { (void)dummy; x = plus1(x); }
391 inline cl_LF& operator-= (cl_LF& x, const cl_LF& y) { return x = x - y; }
392 inline cl_LF& operator-- /* prefix */ (cl_LF& x) { return x = minus1(x); }
393 inline void operator-- /* postfix */ (cl_LF& x, int dummy) { (void)dummy; x = minus1(x); }
394 inline cl_LF& operator*= (cl_LF& x, const cl_LF& y) { return x = x * y; }
395 inline cl_LF& operator/= (cl_LF& x, const cl_LF& y) { return x = x / y; }
396 #endif
397
398
399 // Runtime typing support.
400 extern cl_class cl_class_lfloat;
401
402
403 // Debugging support.
404 #ifdef CL_DEBUG
405 extern int cl_LF_debug_module;
406 CL_FORCE_LINK(cl_LF_debug_dummy, cl_LF_debug_module)
407 #endif
408
409 }  // namespace cln
410
411 #endif /* _CL_LFLOAT_H */