1 // Public double float operations.
6 #include "cln/number.h"
7 #include "cln/dfloat_class.h"
8 #include "cln/integer_class.h"
13 CL_DEFINE_AS_CONVERSION(cl_DF)
16 // Liefert zu einem Double-Float x : (- x), ein DF.
17 extern const cl_DF operator- (const cl_DF& x);
19 // compare(x,y) vergleicht zwei Double-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_DF& x, const cl_DF& y);
23 // equal_hashcode(x) liefert einen equal-invarianten Hashcode für x.
24 extern uint32 equal_hashcode (const cl_DF& x);
26 inline bool operator== (const cl_DF& x, const cl_DF& y)
27 { return compare(x,y)==0; }
28 inline bool operator!= (const cl_DF& x, const cl_DF& y)
29 { return compare(x,y)!=0; }
30 inline bool operator<= (const cl_DF& x, const cl_DF& y)
31 { return compare(x,y)<=0; }
32 inline bool operator< (const cl_DF& x, const cl_DF& y)
33 { return compare(x,y)<0; }
34 inline bool operator>= (const cl_DF& x, const cl_DF& y)
35 { return compare(x,y)>=0; }
36 inline bool operator> (const cl_DF& x, const cl_DF& y)
37 { return compare(x,y)>0; }
39 // minusp(x) == (< x 0)
40 extern bool minusp (const cl_DF& x);
42 // zerop(x) stellt fest, ob ein Double-Float x = 0.0 ist.
43 extern bool zerop (const cl_DF& x);
45 // plusp(x) == (> x 0)
46 extern bool plusp (const cl_DF& x);
48 // Liefert zu zwei Double-Float x und y : (+ x y), ein DF.
49 extern const cl_DF operator+ (const cl_DF& x, const cl_DF& y);
50 // The C++ compiler may hesitate to do these conversions of its own:
51 inline const cl_DF operator+ (const cl_DF& x, const double y)
52 { return x + cl_DF(y); }
53 inline const cl_DF operator+ (const double x, const cl_DF& y)
54 { return cl_DF(x) + y; }
56 // Liefert zu zwei Double-Float x und y : (- x y), ein DF.
57 extern const cl_DF operator- (const cl_DF& x, const cl_DF& y);
58 // The C++ compiler may hesitate to do these conversions of its own:
59 inline const cl_DF operator- (const cl_DF& x, const double y)
60 { return x - cl_DF(y); }
61 inline const cl_DF operator- (const double x, const cl_DF& y)
62 { return cl_DF(x) - y; }
64 // Liefert zu zwei Double-Float x und y : (* x y), ein DF.
65 extern const cl_DF operator* (const cl_DF& x, const cl_DF& y);
66 // The C++ compiler may hesitate to do these conversions of its own:
67 inline const cl_DF operator* (const cl_DF& x, const double y)
68 { return x * cl_DF(y); }
69 inline const cl_DF operator* (const double x, const cl_DF& y)
70 { return cl_DF(x) * y; }
72 // Liefert zu einem Double-Float x : (* x x), ein DF.
73 inline const cl_DF square (const cl_DF& x) { return x*x; }
75 // Liefert zu zwei Double-Float x und y : (/ x y), ein DF.
76 extern const cl_DF operator/ (const cl_DF& x, const cl_DF& y);
77 // The C++ compiler may hesitate to do these conversions of its own:
78 inline const cl_DF operator/ (const cl_DF& x, const double y)
79 { return x / cl_DF(y); }
80 inline const cl_DF operator/ (const double x, const cl_DF& y)
81 { return cl_DF(x) / y; }
83 // Liefert zu einem Double-Float x>=0 : (sqrt x), ein DF.
84 extern const cl_DF sqrt (const cl_DF& x);
86 // recip(x) liefert (/ x), wo x ein Double-Float ist.
87 extern const cl_DF recip (const cl_DF& x);
89 // abs(x) liefert (abs x), wo x ein Double-Float ist.
90 extern const cl_DF abs (const cl_DF& x);
93 // (1+ x), wo x ein Double-Float ist.
94 inline const cl_DF plus1 (const cl_DF& x)
96 extern const cl_DF cl_I_to_DF (const cl_I&);
97 return x + cl_I_to_DF(cl_I(1));
100 // (1- x), wo x ein Double-Float ist.
101 inline const cl_DF minus1 (const cl_DF& x)
103 extern const cl_DF cl_I_to_DF (const cl_I&);
104 return x + cl_I_to_DF(cl_I(-1));
108 // ffloor(x) liefert (ffloor x), wo x ein DF ist.
109 extern const cl_DF ffloor (const cl_DF& x);
111 // fceiling(x) liefert (fceiling x), wo x ein DF ist.
112 extern const cl_DF fceiling (const cl_DF& x);
114 // ftruncate(x) liefert (ftruncate x), wo x ein DF ist.
115 extern const cl_DF ftruncate (const cl_DF& x);
117 // fround(x) liefert (fround x), wo x ein DF ist.
118 extern const cl_DF fround (const cl_DF& x);
121 // Return type for frounding operators.
122 // x / y --> (q,r) with x = y*q+r.
123 struct cl_DF_fdiv_t {
128 cl_DF_fdiv_t (const cl_DF& q, const cl_DF& r) : quotient(q), remainder(r) {}
131 // ffloor2(x) liefert (ffloor x), wo x ein DF ist.
132 inline const cl_DF_fdiv_t ffloor2 (const cl_DF& x)
133 { cl_DF q = ffloor(x); return cl_DF_fdiv_t(q,x-q); }
135 // fceiling2(x) liefert (fceiling x), wo x ein DF ist.
136 inline const cl_DF_fdiv_t fceiling2 (const cl_DF& x)
137 { cl_DF q = fceiling(x); return cl_DF_fdiv_t(q,x-q); }
139 // ftruncate2(x) liefert (ftruncate x), wo x ein DF ist.
140 inline const cl_DF_fdiv_t ftruncate2 (const cl_DF& x)
141 { cl_DF q = ftruncate(x); return cl_DF_fdiv_t(q,x-q); }
143 // fround2(x) liefert (fround x), wo x ein DF ist.
144 inline const cl_DF_fdiv_t fround2 (const cl_DF& x)
145 { cl_DF q = fround(x); return cl_DF_fdiv_t(q,x-q); }
148 // Return type for rounding operators.
149 // x / y --> (q,r) with x = y*q+r.
155 cl_DF_div_t (const cl_I& q, const cl_DF& r) : quotient(q), remainder(r) {}
158 // floor2(x) liefert (floor x), wo x ein DF ist.
159 inline const cl_DF_div_t floor2 (const cl_DF& x)
161 extern const cl_I cl_DF_to_I (const cl_DF& x);
163 return cl_DF_div_t(cl_DF_to_I(q),x-q);
165 inline const cl_I floor1 (const cl_DF& x)
167 extern const cl_I cl_DF_to_I (const cl_DF& x);
168 return cl_DF_to_I(ffloor(x));
171 // ceiling2(x) liefert (ceiling x), wo x ein DF ist.
172 inline const cl_DF_div_t ceiling2 (const cl_DF& x)
174 extern const cl_I cl_DF_to_I (const cl_DF& x);
175 cl_DF q = fceiling(x);
176 return cl_DF_div_t(cl_DF_to_I(q),x-q);
178 inline const cl_I ceiling1 (const cl_DF& x)
180 extern const cl_I cl_DF_to_I (const cl_DF& x);
181 return cl_DF_to_I(fceiling(x));
184 // truncate2(x) liefert (truncate x), wo x ein DF ist.
185 inline const cl_DF_div_t truncate2 (const cl_DF& x)
187 extern const cl_I cl_DF_to_I (const cl_DF& x);
188 cl_DF q = ftruncate(x);
189 return cl_DF_div_t(cl_DF_to_I(q),x-q);
191 inline const cl_I truncate1 (const cl_DF& x)
193 extern const cl_I cl_DF_to_I (const cl_DF& x);
194 return cl_DF_to_I(ftruncate(x));
197 // round2(x) liefert (round x), wo x ein DF ist.
198 inline const cl_DF_div_t round2 (const cl_DF& x)
200 extern const cl_I cl_DF_to_I (const cl_DF& x);
202 return cl_DF_div_t(cl_DF_to_I(q),x-q);
204 inline const cl_I round1 (const cl_DF& x)
206 extern const cl_I cl_DF_to_I (const cl_DF& x);
207 return cl_DF_to_I(fround(x));
210 // floor2(x,y) liefert (floor x y).
211 extern const cl_DF_div_t floor2 (const cl_DF& x, const cl_DF& y);
212 inline const cl_I floor1 (const cl_DF& x, const cl_DF& y) { return floor1(x/y); }
214 // ceiling2(x,y) liefert (ceiling x y).
215 extern const cl_DF_div_t ceiling2 (const cl_DF& x, const cl_DF& y);
216 inline const cl_I ceiling1 (const cl_DF& x, const cl_DF& y) { return ceiling1(x/y); }
218 // truncate2(x,y) liefert (truncate x y).
219 extern const cl_DF_div_t truncate2 (const cl_DF& x, const cl_DF& y);
220 inline const cl_I truncate1 (const cl_DF& x, const cl_DF& y) { return truncate1(x/y); }
222 // round2(x,y) liefert (round x y).
223 extern const cl_DF_div_t round2 (const cl_DF& x, const cl_DF& y);
224 inline const cl_I round1 (const cl_DF& x, const cl_DF& y) { return round1(x/y); }
227 // Return type for decode_float:
228 struct decoded_dfloat {
234 decoded_dfloat (const cl_DF& m, const cl_I& e, const cl_DF& s) : mantissa(m), exponent(e), sign(s) {}
237 // decode_float(x) liefert zu einem Float x: (decode-float x).
238 // x = 0.0 liefert (0.0, 0, 1.0).
239 // x = (-1)^s * 2^e * m liefert ((-1)^0 * 2^0 * m, e als Integer, (-1)^s).
240 extern const decoded_dfloat decode_float (const cl_DF& x);
242 // float_exponent(x) liefert zu einem Float x:
243 // den Exponenten von (decode-float x).
244 // x = 0.0 liefert 0.
245 // x = (-1)^s * 2^e * m liefert e.
246 extern sintE float_exponent (const cl_DF& x);
248 // float_radix(x) liefert (float-radix x), wo x ein Float ist.
249 inline sintL float_radix (const cl_DF& x)
255 // float_sign(x) liefert (float-sign x), wo x ein Float ist.
256 extern const cl_DF float_sign (const cl_DF& x);
258 // float_digits(x) liefert (float-digits x), wo x ein Float ist.
259 // < ergebnis: ein uintC >0
260 extern uintC float_digits (const cl_DF& x);
262 // float_precision(x) liefert (float-precision x), wo x ein Float ist.
263 // < ergebnis: ein uintC >=0
264 extern uintC float_precision (const cl_DF& x);
267 // integer_decode_float(x) liefert zu einem Float x: (integer-decode-float x).
268 // x = 0.0 liefert (0, 0, 1).
269 // x = (-1)^s * 2^e * m bei Float-Precision p liefert
270 // (Mantisse 2^p * m als Integer, e-p als Integer, (-1)^s als Fixnum).
271 extern const cl_idecoded_float integer_decode_float (const cl_DF& x);
274 // scale_float(x,delta) liefert x*2^delta, wo x ein DF ist.
275 extern const cl_DF scale_float (const cl_DF& x, sintC delta);
276 extern const cl_DF scale_float (const cl_DF& x, const cl_I& delta);
279 // max(x,y) liefert (max x y), wo x und y Floats sind.
280 extern const cl_DF max (const cl_DF& x, const cl_DF& y);
282 // min(x,y) liefert (min x y), wo x und y Floats sind.
283 extern const cl_DF min (const cl_DF& x, const cl_DF& y);
285 // signum(x) liefert (signum x), wo x ein Float ist.
286 extern const cl_DF signum (const cl_DF& x);
289 // Konversion zu einem C "float".
290 extern float float_approx (const cl_DF& x);
292 // Konversion zu einem C "double".
293 extern double double_approx (const cl_DF& x);
296 // This could be optimized to use in-place operations.
297 inline cl_DF& operator+= (cl_DF& x, const cl_DF& y) { return x = x + y; }
298 inline cl_DF& operator+= (cl_DF& x, const double y) { return x = x + y; }
299 inline cl_DF& operator++ /* prefix */ (cl_DF& x) { return x = plus1(x); }
300 inline void operator++ /* postfix */ (cl_DF& x, int dummy) { (void)dummy; x = plus1(x); }
301 inline cl_DF& operator-= (cl_DF& x, const cl_DF& y) { return x = x - y; }
302 inline cl_DF& operator-= (cl_DF& x, const double y) { return x = x - y; }
303 inline cl_DF& operator-- /* prefix */ (cl_DF& x) { return x = minus1(x); }
304 inline void operator-- /* postfix */ (cl_DF& x, int dummy) { (void)dummy; x = minus1(x); }
305 inline cl_DF& operator*= (cl_DF& x, const cl_DF& y) { return x = x * y; }
306 inline cl_DF& operator*= (cl_DF& x, const double y) { return x = x * y; }
307 inline cl_DF& operator/= (cl_DF& x, const cl_DF& y) { return x = x / y; }
308 inline cl_DF& operator/= (cl_DF& x, const double y) { return x = x / y; }
315 // Runtime typing support.
316 extern cl_class cl_class_dfloat;
319 // Debugging support.
321 extern int cl_DF_debug_module;
322 CL_FORCE_LINK(cl_DF_debug_dummy, cl_DF_debug_module)
327 #endif /* _CL_DFLOAT_H */