1 // Public long float operations.
6 #include "cln/number.h"
7 #include "cln/lfloat_class.h"
8 #include "cln/integer_class.h"
13 CL_DEFINE_AS_CONVERSION(cl_LF)
16 // Liefert zu einem Long-Float x : (- x), ein LF.
17 extern const cl_LF operator- (const cl_LF& x);
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);
23 // equal_hashcode(x) liefert einen equal-invarianten Hashcode für x.
24 extern uint32 equal_hashcode (const cl_LF& x);
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; }
39 // minusp(x) == (< x 0)
40 extern bool minusp (const cl_LF& x);
42 // zerop(x) stellt fest, ob ein Long-Float x = 0.0 ist.
43 extern bool zerop (const cl_LF& x);
45 // plusp(x) == (> x 0)
46 extern bool plusp (const cl_LF& x);
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);
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);
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)
59 extern const cl_R cl_LF_I_mul (const cl_LF&, const cl_I&);
60 return cl_LF_I_mul(x,y);
62 inline const cl_R operator* (const cl_I& x, const cl_LF& y)
64 extern const cl_R cl_LF_I_mul (const cl_LF&, const cl_I&);
65 return cl_LF_I_mul(y,x);
67 inline const cl_R operator* (const cl_LF& x, const cl_RA& y)
69 extern const cl_R cl_LF_RA_mul (const cl_LF&, const cl_RA&);
70 return cl_LF_RA_mul(x,y);
72 inline const cl_R operator* (const cl_RA& x, const cl_LF& y)
74 extern const cl_R cl_LF_RA_mul (const cl_LF&, const cl_RA&);
75 return cl_LF_RA_mul(y,x);
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; }
87 inline const cl_R operator* (const long long x, const cl_LF& y)
88 { return cl_I(x) * y; }
89 inline const cl_R operator* (const unsigned long long x, const cl_LF& y)
90 { return cl_I(x) * y; }
92 inline const cl_R operator* (const cl_LF& x, const int y)
93 { return x * cl_I(y); }
94 inline const cl_R operator* (const cl_LF& x, const unsigned int y)
95 { return x * cl_I(y); }
96 inline const cl_R operator* (const cl_LF& x, const long y)
97 { return x * cl_I(y); }
98 inline const cl_R operator* (const cl_LF& x, const unsigned long y)
99 { return x * cl_I(y); }
101 inline const cl_R operator* (const cl_LF& x, const long long y)
102 { return x * cl_I(y); }
103 inline const cl_R operator* (const cl_LF& x, const unsigned long long y)
104 { return x * cl_I(y); }
106 // Spezialfall x = y.
107 // Liefert zu einem Long-Float x : (* x x), ein LF.
108 extern const cl_LF square (const cl_LF& x);
110 // Liefert zu zwei Long-Float x und y : (/ x y), ein LF.
111 extern const cl_LF operator/ (const cl_LF& x, const cl_LF& y);
112 // Spezialfall x oder y Integer oder rationale Zahl.
113 inline const cl_LF operator/ (const cl_LF& x, const cl_I& y)
115 extern const cl_LF cl_LF_I_div (const cl_LF& x, const cl_I& y);
116 return cl_LF_I_div(x,y);
118 inline const cl_R operator/ (const cl_I& x, const cl_LF& y)
120 extern const cl_R cl_I_LF_div (const cl_I& x, const cl_LF& y);
121 return cl_I_LF_div(x,y);
123 inline const cl_LF operator/ (const cl_LF& x, const cl_RA& y)
125 extern const cl_LF cl_LF_RA_div (const cl_LF& x, const cl_RA& y);
126 return cl_LF_RA_div(x,y);
128 inline const cl_R operator/ (const cl_RA& x, const cl_LF& y)
130 extern const cl_R cl_RA_LF_div (const cl_RA& x, const cl_LF& y);
131 return cl_RA_LF_div(x,y);
133 // Dem C++-Compiler muß man nun auch das Folgende sagen:
134 inline const cl_LF operator/ (const cl_LF& x, const int y)
135 { return x / cl_I(y); }
136 inline const cl_LF operator/ (const cl_LF& x, const unsigned int y)
137 { return x / cl_I(y); }
138 inline const cl_LF operator/ (const cl_LF& x, const long y)
139 { return x / cl_I(y); }
140 inline const cl_LF operator/ (const cl_LF& x, const unsigned long y)
141 { return x / cl_I(y); }
143 inline const cl_LF operator/ (const cl_LF& x, const long long y)
144 { return x / cl_I(y); }
145 inline const cl_LF operator/ (const cl_LF& x, const unsigned long long y)
146 { return x / cl_I(y); }
148 inline const cl_R operator/ (const int x, const cl_LF& y)
149 { return cl_I(x) / y; }
150 inline const cl_R operator/ (const unsigned int x, const cl_LF& y)
151 { return cl_I(x) / y; }
152 inline const cl_R operator/ (const long x, const cl_LF& y)
153 { return cl_I(x) / y; }
154 inline const cl_R operator/ (const unsigned long x, const cl_LF& y)
155 { return cl_I(x) / y; }
157 inline const cl_R operator/ (const long long x, const cl_LF& y)
158 { return cl_I(x) / y; }
159 inline const cl_R operator/ (const unsigned long long x, const cl_LF& y)
160 { return cl_I(x) / y; }
163 // Liefert zu einem Long-Float x>=0 : (sqrt x), ein LF.
164 extern const cl_LF sqrt (const cl_LF& x);
166 // recip(x) liefert (/ x), wo x ein Long-Float ist.
167 extern const cl_LF recip (const cl_LF& x);
169 // abs(x) liefert (abs x), wo x ein Long-Float ist.
170 extern const cl_LF abs (const cl_LF& x);
173 // (1+ x), wo x ein Long-Float ist.
174 extern const cl_LF plus1 (const cl_LF& x);
176 // (1- x), wo x ein Long-Float ist.
177 extern const cl_LF minus1 (const cl_LF& x);
180 // ffloor(x) liefert (ffloor x), wo x ein LF ist.
181 extern const cl_LF ffloor (const cl_LF& x);
183 // fceiling(x) liefert (fceiling x), wo x ein LF ist.
184 extern const cl_LF fceiling (const cl_LF& x);
186 // ftruncate(x) liefert (ftruncate x), wo x ein LF ist.
187 extern const cl_LF ftruncate (const cl_LF& x);
189 // fround(x) liefert (fround x), wo x ein LF ist.
190 extern const cl_LF fround (const cl_LF& x);
193 // Return type for frounding operators.
194 // x / y --> (q,r) with x = y*q+r.
195 struct cl_LF_fdiv_t {
200 cl_LF_fdiv_t (const cl_LF& q, const cl_LF& r) : quotient(q), remainder(r) {}
203 // ffloor2(x) liefert (ffloor x), wo x ein LF ist.
204 inline const cl_LF_fdiv_t ffloor2 (const cl_LF& x)
206 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
208 return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
211 // fceiling2(x) liefert (fceiling x), wo x ein LF ist.
212 inline const cl_LF_fdiv_t fceiling2 (const cl_LF& x)
214 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
215 cl_LF q = fceiling(x);
216 return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
219 // ftruncate2(x) liefert (ftruncate x), wo x ein LF ist.
220 inline const cl_LF_fdiv_t ftruncate2 (const cl_LF& x)
222 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
223 cl_LF q = ftruncate(x);
224 return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
227 // fround2(x) liefert (fround x), wo x ein LF ist.
228 inline const cl_LF_fdiv_t fround2 (const cl_LF& x)
230 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
232 return cl_LF_fdiv_t(q,LF_LF_minus_LF(x,q));
236 // Return type for rounding operators.
237 // x / y --> (q,r) with x = y*q+r.
243 cl_LF_div_t (const cl_I& q, const cl_LF& r) : quotient(q), remainder(r) {}
246 // floor2(x) liefert (floor x), wo x ein LF ist.
247 inline const cl_LF_div_t floor2 (const cl_LF& x)
249 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
250 extern const cl_I cl_LF_to_I (const cl_LF& x);
252 return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
254 inline const cl_I floor1 (const cl_LF& x)
256 extern const cl_I cl_LF_to_I (const cl_LF& x);
257 return cl_LF_to_I(ffloor(x));
260 // ceiling2(x) liefert (ceiling x), wo x ein LF ist.
261 inline const cl_LF_div_t ceiling2 (const cl_LF& x)
263 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
264 extern const cl_I cl_LF_to_I (const cl_LF& x);
265 cl_LF q = fceiling(x);
266 return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
268 inline const cl_I ceiling1 (const cl_LF& x)
270 extern const cl_I cl_LF_to_I (const cl_LF& x);
271 return cl_LF_to_I(fceiling(x));
274 // truncate2(x) liefert (truncate x), wo x ein LF ist.
275 inline const cl_LF_div_t truncate2 (const cl_LF& x)
277 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
278 extern const cl_I cl_LF_to_I (const cl_LF& x);
279 cl_LF q = ftruncate(x);
280 return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
282 inline const cl_I truncate1 (const cl_LF& x)
284 extern const cl_I cl_LF_to_I (const cl_LF& x);
285 return cl_LF_to_I(ftruncate(x));
288 // round2(x) liefert (round x), wo x ein LF ist.
289 inline const cl_LF_div_t round2 (const cl_LF& x)
291 extern const cl_LF LF_LF_minus_LF (const cl_LF&, const cl_LF&);
292 extern const cl_I cl_LF_to_I (const cl_LF& x);
294 return cl_LF_div_t(cl_LF_to_I(q),LF_LF_minus_LF(x,q));
296 inline const cl_I round1 (const cl_LF& x)
298 extern const cl_I cl_LF_to_I (const cl_LF& x);
299 return cl_LF_to_I(fround(x));
302 // floor2(x,y) liefert (floor x y).
303 extern const cl_LF_div_t floor2 (const cl_LF& x, const cl_LF& y);
304 inline const cl_I floor1 (const cl_LF& x, const cl_LF& y) { return floor1(x/y); }
306 // ceiling2(x,y) liefert (ceiling x y).
307 extern const cl_LF_div_t ceiling2 (const cl_LF& x, const cl_LF& y);
308 inline const cl_I ceiling1 (const cl_LF& x, const cl_LF& y) { return ceiling1(x/y); }
310 // truncate2(x,y) liefert (truncate x y).
311 extern const cl_LF_div_t truncate2 (const cl_LF& x, const cl_LF& y);
312 inline const cl_I truncate1 (const cl_LF& x, const cl_LF& y) { return truncate1(x/y); }
314 // round2(x,y) liefert (round x y).
315 extern const cl_LF_div_t round2 (const cl_LF& x, const cl_LF& y);
316 inline const cl_I round1 (const cl_LF& x, const cl_LF& y) { return round1(x/y); }
319 // cl_float(x,y) returns a long float if y is a long float.
320 inline const cl_LF cl_float (const cl_F& x, const cl_LF& y)
322 extern const cl_F cl_float (const cl_F& x, const cl_F& y);
323 return The(cl_LF)(cl_float(x,(const cl_F&)y));
325 inline const cl_LF cl_float (const cl_I& x, const cl_LF& y)
327 extern const cl_F cl_float (const cl_I& x, const cl_F& y);
328 return The(cl_LF)(cl_float(x,(const cl_F&)y));
330 inline const cl_LF cl_float (const cl_RA& x, const cl_LF& y)
332 extern const cl_F cl_float (const cl_RA& x, const cl_F& y);
333 return The(cl_LF)(cl_float(x,(const cl_F&)y));
335 inline const cl_LF cl_float (int x, const cl_LF& y)
336 { return cl_float(cl_I(x),y); }
337 inline const cl_LF cl_float (unsigned int x, const cl_LF& y)
338 { return cl_float(cl_I(x),y); }
341 // Return type for decode_float:
342 struct decoded_lfloat {
348 decoded_lfloat (const cl_LF& m, const cl_I& e, const cl_LF& s) : mantissa(m), exponent(e), sign(s) {}
351 // decode_float(x) liefert zu einem Float x: (decode-float x).
352 // x = 0.0 liefert (0.0, 0, 1.0).
353 // x = (-1)^s * 2^e * m liefert ((-1)^0 * 2^0 * m, e als Integer, (-1)^s).
354 extern const decoded_lfloat decode_float (const cl_LF& x);
356 // float_exponent(x) liefert zu einem Float x:
357 // den Exponenten von (decode-float x).
358 // x = 0.0 liefert 0.
359 // x = (-1)^s * 2^e * m liefert e.
360 extern sintE float_exponent (const cl_LF& x);
362 // float_radix(x) liefert (float-radix x), wo x ein Float ist.
363 inline sintL float_radix (const cl_LF& x)
369 // float_sign(x) liefert (float-sign x), wo x ein Float ist.
370 extern const cl_LF float_sign (const cl_LF& x);
372 // float_digits(x) liefert (float-digits x), wo x ein Float ist.
373 // < ergebnis: ein uintC >0
374 extern uintC float_digits (const cl_LF& x);
376 // float_precision(x) liefert (float-precision x), wo x ein Float ist.
377 // < ergebnis: ein uintC >=0
378 extern uintC float_precision (const cl_LF& x);
381 // integer_decode_float(x) liefert zu einem Float x: (integer-decode-float x).
382 // x = 0.0 liefert (0, 0, 1).
383 // x = (-1)^s * 2^e * m bei Float-Precision p liefert
384 // (Mantisse 2^p * m als Integer, e-p als Integer, (-1)^s als Fixnum).
385 extern const cl_idecoded_float integer_decode_float (const cl_LF& x);
388 // scale_float(x,delta) liefert x*2^delta, wo x ein LF ist.
389 extern const cl_LF scale_float (const cl_LF& x, sintC delta);
390 extern const cl_LF scale_float (const cl_LF& x, const cl_I& delta);
393 // max(x,y) liefert (max x y), wo x und y Floats sind.
394 extern const cl_LF max (const cl_LF& x, const cl_LF& y);
396 // min(x,y) liefert (min x y), wo x und y Floats sind.
397 extern const cl_LF min (const cl_LF& x, const cl_LF& y);
399 // signum(x) liefert (signum x), wo x ein Float ist.
400 extern const cl_LF signum (const cl_LF& x);
403 // Konversion zu einem C "float".
404 extern float float_approx (const cl_LF& x);
406 // Konversion zu einem C "double".
407 extern double double_approx (const cl_LF& x);
410 // This could be optimized to use in-place operations.
411 inline cl_LF& operator+= (cl_LF& x, const cl_LF& y) { return x = x + y; }
412 inline cl_LF& operator++ /* prefix */ (cl_LF& x) { return x = plus1(x); }
413 inline void operator++ /* postfix */ (cl_LF& x, int dummy) { (void)dummy; x = plus1(x); }
414 inline cl_LF& operator-= (cl_LF& x, const cl_LF& y) { return x = x - y; }
415 inline cl_LF& operator-- /* prefix */ (cl_LF& x) { return x = minus1(x); }
416 inline void operator-- /* postfix */ (cl_LF& x, int dummy) { (void)dummy; x = minus1(x); }
417 inline cl_LF& operator*= (cl_LF& x, const cl_LF& y) { return x = x * y; }
418 inline cl_LF& operator/= (cl_LF& x, const cl_LF& y) { return x = x / y; }
421 // Runtime typing support.
422 extern cl_class cl_class_lfloat;
425 // Debugging support.
427 extern int cl_LF_debug_module;
428 CL_FORCE_LINK(cl_LF_debug_dummy, cl_LF_debug_module)
433 #endif /* _CL_LFLOAT_H */