12 #include "cln/float.h"
15 #include "cln/lfloat.h"
17 #include "cln/integer.h"
20 #define MAYBE_INLINE inline
21 #include "cl_LF_zerop.cc"
22 #include "cl_LF_exponent.cc"
26 // cl_F expx_naive (const cl_F& x)
27 // cl_LF expx_naive (const cl_LF& x)
30 // e := Exponent aus (decode-float x), d := (float-digits x)
31 // Bei x=0.0 oder e<-d liefere 1.0
32 // (denn bei e<=-d-1 ist abs(exp(x)-1) = abs(x)+O(x^2) < 2^(-d-1),
33 // also ist exp(x), auf d Bits gerundet, gleich 1.0).
34 // Bei e<=-sqrt(d) verwende die Potenzreihe
35 // exp(x) = sum(j=0..inf,x^j/j!):
36 // b:=1, i:=0, sum:=0,
37 // while (/= sum (setq sum (+ sum b))) do b:=b*x/(i+1), i:=i+1.
39 // Sonst setze y := x/2 = (scale-float x -1),
40 // berechne rekursiv z:=exp(y) und liefere z^2.
41 // Aufwand: asymptotisch d^2.5 .
43 const cl_LF expx_naive (const cl_LF& x)
46 // wie oben, mit adaptiver Genauigkeit während der Potenzreihen-Summation.
49 var uintL actuallen = TheLfloat(x)->len;
50 var uintC d = float_digits(x);
51 var sintL e = float_exponent(x);
52 if (e < -(sintC)d) // e < -d ?
53 return cl_float(1,x); // ja -> 1.0 als Ergebnis
55 var uintL k = 0; // Rekursionszähler k:=0
56 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
57 // angewandt werden. limit_slope = 1.0 ist nicht schlecht,
58 // auch im Bereich d = ca. 800.
59 var sintL e_limit = -1-isqrt(d); // -1-floor(sqrt(d))
61 // e > -1-floor(sqrt(d)) -> muß |x| verkleinern.
63 x = scale_float(x,-(sintL)k); // x := x/2^k
64 // Neuer Exponent = e-k = e_limit.
66 // Potenzreihe anwenden:
68 var cl_LF b = cl_float(1,x); // b := (float 1 x)
69 var cl_LF eps = scale_float(b,-(sintC)d-10);
70 var cl_LF sum = cl_float(0,x); // sum := (float 0 x)
72 var cl_LF new_sum = sum + LF_to_LF(b,actuallen);
73 if (new_sum == sum) // = sum ?
74 break; // ja -> Potenzreihe abbrechen
77 b = cl_LF_shortenwith(b,eps);
78 b = (b*x)/(cl_I)i; // b := b*x/i
80 var cl_LF& result = sum; // sum als Ergebnis
81 // Wegen Rekursion noch k mal quadrieren:
83 result = square(result);
86 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
88 const cl_F expx_naive (const cl_F& x)
96 var uintC d = float_digits(x);
97 var sintL e = float_exponent(x);
98 if (e < -(sintC)d) // e < -d ?
99 return cl_float(1,x); // ja -> 1.0 als Ergebnis
101 var uintL k = 0; // Rekursionszähler k:=0
102 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
103 // angewandt werden. limit_slope = 1.0 ist nicht schlecht. Für
104 // d > 1600 scheint der Bereich 2.0 <= limit_slope <= 2.6 am besten
105 // zu sein (mit bis zu 15% Beschleunigung gegenüber limit_slope = 1.0),
106 // aber in diesem Bereich rechnen wir gar nicht.
107 // Wir wählen limit_slope = 1.5.
108 var sintL e_limit = -1-floor(isqrt(d)*3,2); // -1-floor(sqrt(d))
110 // e > -1-floor(sqrt(d)) -> muß |x| verkleinern.
112 x = scale_float(x,-(sintL)k); // x := x/2^k
113 // Neuer Exponent = e-k = e_limit.
115 // Potenzreihe anwenden:
117 var cl_F b = cl_float(1,x); // b := (float 1 x)
118 var cl_F sum = cl_float(0,x); // sum := (float 0 x)
120 var cl_F new_sum = sum + b;
121 if (new_sum == sum) // = sum ?
122 break; // ja -> Potenzreihe abbrechen
125 b = (b*x)/(cl_I)i; // b := b*x/i
127 var cl_F& result = sum; // sum als Ergebnis
128 // Wegen Rekursion noch k mal quadrieren:
130 result = square(result);
133 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
135 const cl_LF expx_ratseries (const cl_LF& x)
137 // [Jonathan M. Borwein, Peter B. Borwein: Pi and the AGM.
138 // Wiley 1987. Section 10.2.3]
139 var uintC len = TheLfloat(x)->len;
140 var cl_idecoded_float x_ = integer_decode_float(x);
141 // x = (-1)^sign * 2^exponent * mantissa
142 var uintL lq = cl_I_to_UL(- x_.exponent);
143 var const cl_I& p = x_.mantissa;
144 // Compute exp(p/2^lq) by splitting into pieces.
145 // Each piece gives rise to a factor exp(pk/2^lqk).
146 // Instead of the standard choice lqk = 2^k, we choose
147 // lqk = c^k + O(1), where c > 1 is real.
148 // Running time on Linux i486, 33 Mhz, computing exp(sqrt(2)-1):
149 // c 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0 3.1 3.2 3.3 3.4 3.5
150 // (a) 400 393 390 377 371 360 363 367 367 358 362 362 363 362 376 372
151 // (b) 311 317 305 312 295 291 286 293 291 284 295 284 293 287 288 305
152 // (a): N=300, time in 0.01 sec. (b): N=1000, time in 0.1 sec.
153 // Values 2.5 <= c <= 3.2 seem best. Let's choose c = 2.875.
154 var cl_boolean first_factor = cl_true;
158 for (b1 = 0, b2 = 1; b1 < lq; b1 = b2, b2 = ceiling(b2*23,8)) {
159 // Piece containing bits b1+1..b2 after "decimal point"
160 // in the binary representation of (p/2^lq).
161 var uintL lqk = (lq >= b2 ? b2 : lq);
162 var cl_I pk = ldb(p,cl_byte(lqk-b1,lq-lqk));
163 // Compute exp(pk/2^lqk).
165 if (minusp(x_.sign)) { pk = -pk; }
166 var cl_LF factor = cl_exp_aux(pk,lqk,len);
169 first_factor = cl_false;
171 product = product * factor;
175 return cl_I_to_LF(1,len);
179 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
181 // Timings of the above algorithms, on an i486 33 MHz, running Linux,
182 // applied to x = sqrt(2)-1 = 0.414...
183 // ("naive" with adaptive limit_slope, about sqrt(ln(len)).)
192 // ==> ratseries faster for N >= 84.