]> www.ginac.de Git - cln.git/blob - include/cln/dfloat.h
de1053817f90627ec2df0106763fff4ac11f5c0b
[cln.git] / include / cln / dfloat.h
1 // Public double float operations.
2
3 #ifndef _CL_DFLOAT_H
4 #define _CL_DFLOAT_H
5
6 #include "cln/number.h"
7 #include "cln/dfloat_class.h"
8 #include "cln/integer_class.h"
9 #include "cln/float.h"
10
11 namespace cln {
12
13 CL_DEFINE_AS_CONVERSION(cl_DF)
14
15
16 // Liefert zu einem Double-Float x : (- x), ein DF.
17 extern const cl_DF operator- (const cl_DF& x);
18
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);
22
23 // equal_hashcode(x) liefert einen equal-invarianten Hashcode für x.
24 extern uint32 equal_hashcode (const cl_DF& x);
25
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; }
38
39 // minusp(x) == (< x 0)
40 extern bool minusp (const cl_DF& x);
41
42 // zerop(x) stellt fest, ob ein Double-Float x = 0.0 ist.
43 extern bool zerop (const cl_DF& x);
44
45 // plusp(x) == (> x 0)
46 extern bool plusp (const cl_DF& x);
47
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; }
55
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; }
63
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; }
71
72 // Liefert zu einem Double-Float x : (* x x), ein DF.
73 inline const cl_DF square (const cl_DF& x) { return x*x; }
74
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; }
82
83 // Liefert zu einem Double-Float x>=0 : (sqrt x), ein DF.
84 extern const cl_DF sqrt (const cl_DF& x);
85
86 // recip(x) liefert (/ x), wo x ein Double-Float ist.
87 extern const cl_DF recip (const cl_DF& x);
88
89 // abs(x) liefert (abs x), wo x ein Double-Float ist.
90 extern const cl_DF abs (const cl_DF& x);
91
92
93 // (1+ x), wo x ein Double-Float ist.
94 inline const cl_DF plus1 (const cl_DF& x)
95 {
96         extern const cl_DF cl_I_to_DF (const cl_I&);
97         return x + cl_I_to_DF(cl_I(1));
98 }
99
100 // (1- x), wo x ein Double-Float ist.
101 inline const cl_DF minus1 (const cl_DF& x)
102 {
103         extern const cl_DF cl_I_to_DF (const cl_I&);
104         return x + cl_I_to_DF(cl_I(-1));
105 }
106
107
108 // ffloor(x) liefert (ffloor x), wo x ein DF ist.
109 extern const cl_DF ffloor (const cl_DF& x);
110
111 // fceiling(x) liefert (fceiling x), wo x ein DF ist.
112 extern const cl_DF fceiling (const cl_DF& x);
113
114 // ftruncate(x) liefert (ftruncate x), wo x ein DF ist.
115 extern const cl_DF ftruncate (const cl_DF& x);
116
117 // fround(x) liefert (fround x), wo x ein DF ist.
118 extern const cl_DF fround (const cl_DF& x);
119
120
121 // Return type for frounding operators.
122 // x / y  --> (q,r) with x = y*q+r.
123 struct cl_DF_fdiv_t {
124         cl_DF quotient;
125         cl_DF remainder;
126 // Constructor.
127         cl_DF_fdiv_t () {}
128         cl_DF_fdiv_t (const cl_DF& q, const cl_DF& r) : quotient(q), remainder(r) {}
129 };
130
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); }
134
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); }
138
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); }
142
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); }
146
147
148 // Return type for rounding operators.
149 // x / y  --> (q,r) with x = y*q+r.
150 struct cl_DF_div_t {
151         cl_I quotient;
152         cl_DF remainder;
153 // Constructor.
154         cl_DF_div_t () {}
155         cl_DF_div_t (const cl_I& q, const cl_DF& r) : quotient(q), remainder(r) {}
156 };
157
158 // floor2(x) liefert (floor x), wo x ein DF ist.
159 inline const cl_DF_div_t floor2 (const cl_DF& x)
160 {
161         extern const cl_I cl_DF_to_I (const cl_DF& x);
162         cl_DF q = ffloor(x);
163         return cl_DF_div_t(cl_DF_to_I(q),x-q);
164 }
165 inline const cl_I floor1 (const cl_DF& x)
166 {
167         extern const cl_I cl_DF_to_I (const cl_DF& x);
168         return cl_DF_to_I(ffloor(x));
169 }
170
171 // ceiling2(x) liefert (ceiling x), wo x ein DF ist.
172 inline const cl_DF_div_t ceiling2 (const cl_DF& x)
173 {
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);
177 }
178 inline const cl_I ceiling1 (const cl_DF& x)
179 {
180         extern const cl_I cl_DF_to_I (const cl_DF& x);
181         return cl_DF_to_I(fceiling(x));
182 }
183
184 // truncate2(x) liefert (truncate x), wo x ein DF ist.
185 inline const cl_DF_div_t truncate2 (const cl_DF& x)
186 {
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);
190 }
191 inline const cl_I truncate1 (const cl_DF& x)
192 {
193         extern const cl_I cl_DF_to_I (const cl_DF& x);
194         return cl_DF_to_I(ftruncate(x));
195 }
196
197 // round2(x) liefert (round x), wo x ein DF ist.
198 inline const cl_DF_div_t round2 (const cl_DF& x)
199 {
200         extern const cl_I cl_DF_to_I (const cl_DF& x);
201         cl_DF q = fround(x);
202         return cl_DF_div_t(cl_DF_to_I(q),x-q);
203 }
204 inline const cl_I round1 (const cl_DF& x)
205 {
206         extern const cl_I cl_DF_to_I (const cl_DF& x);
207         return cl_DF_to_I(fround(x));
208 }
209
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); }
213
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); }
217
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); }
221
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); }
225
226
227 // Return type for decode_float:
228 struct decoded_dfloat {
229         cl_DF mantissa;
230         cl_I exponent;
231         cl_DF sign;
232 // Constructor.
233         decoded_dfloat () {}
234         decoded_dfloat (const cl_DF& m, const cl_I& e, const cl_DF& s) : mantissa(m), exponent(e), sign(s) {}
235 };
236
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);
241
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);
247
248 // float_radix(x) liefert (float-radix x), wo x ein Float ist.
249 inline sintL float_radix (const cl_DF& x)
250 {
251         (void)x; // unused x
252         return 2;
253 }
254
255 // float_sign(x) liefert (float-sign x), wo x ein Float ist.
256 extern const cl_DF float_sign (const cl_DF& x);
257
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);
261
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);
265
266
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);
272
273
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);
277
278
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);
281
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);
284
285 // signum(x) liefert (signum x), wo x ein Float ist.
286 extern const cl_DF signum (const cl_DF& x);
287
288
289 // Konversion zu einem C "float".
290 extern float float_approx (const cl_DF& x);
291
292 // Konversion zu einem C "double".
293 extern double double_approx (const cl_DF& x);
294
295
296 #ifdef WANT_OBFUSCATING_OPERATORS
297 // This could be optimized to use in-place operations.
298 inline cl_DF& operator+= (cl_DF& x, const cl_DF& y) { return x = x + y; }
299 inline cl_DF& operator+= (cl_DF& x, const double y) { return x = x + y; }
300 inline cl_DF& operator++ /* prefix */ (cl_DF& x) { return x = plus1(x); }
301 inline void operator++ /* postfix */ (cl_DF& x, int dummy) { (void)dummy; x = plus1(x); }
302 inline cl_DF& operator-= (cl_DF& x, const cl_DF& y) { return x = x - y; }
303 inline cl_DF& operator-= (cl_DF& x, const double y) { return x = x - y; }
304 inline cl_DF& operator-- /* prefix */ (cl_DF& x) { return x = minus1(x); }
305 inline void operator-- /* postfix */ (cl_DF& x, int dummy) { (void)dummy; x = minus1(x); }
306 inline cl_DF& operator*= (cl_DF& x, const cl_DF& y) { return x = x * y; }
307 inline cl_DF& operator*= (cl_DF& x, const double y) { return x = x * y; }
308 inline cl_DF& operator/= (cl_DF& x, const cl_DF& y) { return x = x / y; }
309 inline cl_DF& operator/= (cl_DF& x, const double y) { return x = x / y; }
310 #endif
311
312
313 /* */
314 CL_REQUIRE(cl_ieee)
315
316
317 // Runtime typing support.
318 extern cl_class cl_class_dfloat;
319
320
321 // Debugging support.
322 #ifdef CL_DEBUG
323 extern int cl_DF_debug_module;
324 CL_FORCE_LINK(cl_DF_debug_dummy, cl_DF_debug_module)
325 #endif
326
327 }  // namespace cln
328
329 #endif /* _CL_DFLOAT_H */