]> www.ginac.de Git - cln.git/blob - src/float/transcendental/cl_F_expx.cc
104c621bb4a8e1a2c9935662302ef19d9ec296aa
[cln.git] / src / float / transcendental / cl_F_expx.cc
1 // expx().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_F_tran.h"
8
9
10 // Implementation.
11
12 #include "cl_float.h"
13 #include "cl_low.h"
14 #include "cl_F.h"
15 #include "cl_lfloat.h"
16 #include "cl_LF.h"
17 #include "cl_integer.h"
18
19 #undef MAYBE_INLINE
20 #define MAYBE_INLINE inline
21 #include "cl_LF_zerop.cc"
22 #include "cl_LF_exponent.cc"
23
24 // cl_F expx_naive (const cl_F& x)
25 // cl_LF expx_naive (const cl_LF& x)
26 //
27 // Methode:
28 // e := Exponent aus (decode-float x), d := (float-digits x)
29 // Bei x=0.0 oder e<-d liefere 1.0
30 //   (denn bei e<=-d-1 ist abs(exp(x)-1) = abs(x)+O(x^2) < 2^(-d-1),
31 //    also ist exp(x), auf d Bits gerundet, gleich 1.0).
32 // Bei e<=-sqrt(d) verwende die Potenzreihe
33 //   exp(x) = sum(j=0..inf,x^j/j!):
34 //   b:=1, i:=0, sum:=0,
35 //   while (/= sum (setq sum (+ sum b))) do b:=b*x/(i+1), i:=i+1.
36 //   Ergebnis sum.
37 // Sonst setze y := x/2 = (scale-float x -1),
38 //   berechne rekursiv z:=exp(y) und liefere z^2.
39 // Aufwand: asymptotisch d^2.5 .
40
41 const cl_LF expx_naive (const cl_LF& x)
42 {
43 // Methode:
44 // wie oben, mit adaptiver Genauigkeit während der Potenzreihen-Summation.
45         if (zerop(x))
46                 return cl_float(1,x);
47         var uintL actuallen = TheLfloat(x)->len;
48         var uintL d = float_digits(x);
49         var sintL e = float_exponent(x);
50         if (e < -(sintL)d) // e < -d ?
51                 return cl_float(1,x); // ja -> 1.0 als Ergebnis
52  {      Mutable(cl_LF,x);
53         var uintL k = 0; // Rekursionszähler k:=0
54         // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
55         // angewandt werden. limit_slope = 1.0 ist nicht schlecht,
56         // auch im Bereich d = ca. 800.
57         var sintL e_limit = -1-isqrt(d); // -1-floor(sqrt(d))
58         if (e > e_limit) {
59                 // e > -1-floor(sqrt(d)) -> muß |x| verkleinern.
60                 k = e - e_limit;
61                 x = scale_float(x,-(sintL)k); // x := x/2^k
62                 // Neuer Exponent = e-k = e_limit.
63         }
64         // Potenzreihe anwenden:
65         var int i = 0;
66         var cl_LF b = cl_float(1,x); // b := (float 1 x)
67         var cl_LF eps = scale_float(b,-(sintL)d-10);
68         var cl_LF sum = cl_float(0,x); // sum := (float 0 x)
69         loop {
70                 var cl_LF new_sum = sum + LF_to_LF(b,actuallen);
71                 if (new_sum == sum) // = sum ?
72                         break; // ja -> Potenzreihe abbrechen
73                 sum = new_sum;
74                 i = i+1;
75                 b = cl_LF_shortenwith(b,eps);
76                 b = (b*x)/(cl_I)i; // b := b*x/i
77         }
78         var cl_LF& result = sum; // sum als Ergebnis
79         // Wegen Rekursion noch k mal quadrieren:
80         for ( ; k > 0; k--)
81                 result = square(result);
82         return result;
83 }}
84 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
85
86 const cl_F expx_naive (const cl_F& x)
87 {
88         if (longfloatp(x)) {
89                 DeclareType(cl_LF,x);
90                 return expx_naive(x);
91         }
92         if (zerop(x))
93                 return cl_float(1,x);
94         var uintL d = float_digits(x);
95         var sintL e = float_exponent(x);
96         if (e < -(sintL)d) // e < -d ?
97                 return cl_float(1,x); // ja -> 1.0 als Ergebnis
98  {      Mutable(cl_F,x);
99         var uintL k = 0; // Rekursionszähler k:=0
100         // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
101         // angewandt werden. limit_slope = 1.0 ist nicht schlecht. Für
102         // d > 1600 scheint der Bereich 2.0 <= limit_slope <= 2.6 am besten
103         // zu sein (mit bis zu 15% Beschleunigung gegenüber limit_slope = 1.0),
104         // aber in diesem Bereich rechnen wir gar nicht.
105         // Wir wählen limit_slope = 1.5.
106         var sintL e_limit = -1-floor(isqrt(d)*3,2); // -1-floor(sqrt(d))
107         if (e > e_limit) {
108                 // e > -1-floor(sqrt(d)) -> muß |x| verkleinern.
109                 k = e - e_limit;
110                 x = scale_float(x,-(sintL)k); // x := x/2^k
111                 // Neuer Exponent = e-k = e_limit.
112         }
113         // Potenzreihe anwenden:
114         var int i = 0;
115         var cl_F b = cl_float(1,x); // b := (float 1 x)
116         var cl_F sum = cl_float(0,x); // sum := (float 0 x)
117         loop {
118                 var cl_F new_sum = sum + b;
119                 if (new_sum == sum) // = sum ?
120                         break; // ja -> Potenzreihe abbrechen
121                 sum = new_sum;
122                 i = i+1;
123                 b = (b*x)/(cl_I)i; // b := b*x/i
124         }
125         var cl_F& result = sum; // sum als Ergebnis
126         // Wegen Rekursion noch k mal quadrieren:
127         for ( ; k > 0; k--)
128                 result = square(result);
129         return result;
130 }}
131 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
132
133 const cl_LF expx_ratseries (const cl_LF& x)
134 {
135         // [Jonathan M. Borwein, Peter B. Borwein: Pi and the AGM.
136         //  Wiley 1987. Section 10.2.3]
137         var uintC len = TheLfloat(x)->len;
138         var cl_idecoded_float x_ = integer_decode_float(x);
139         // x = (-1)^sign * 2^exponent * mantissa
140         var uintL lq = cl_I_to_UL(- x_.exponent);
141         var const cl_I& p = x_.mantissa;
142         // Compute exp(p/2^lq) by splitting into pieces.
143         // Each piece gives rise to a factor exp(pk/2^lqk).
144         // Instead of the standard choice lqk = 2^k, we choose
145         // lqk = c^k + O(1), where c > 1 is real.
146         // Running time on Linux i486, 33 Mhz, computing exp(sqrt(2)-1):
147         //   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
148         //  (a) 400 393 390 377 371 360 363 367 367 358 362 362 363 362 376 372
149         //  (b) 311 317 305 312 295 291 286 293 291 284 295 284 293 287 288 305
150         // (a): N=300, time in 0.01 sec. (b): N=1000, time in 0.1 sec.
151         // Values 2.5 <= c <= 3.2 seem best. Let's choose c = 2.875.
152         var cl_boolean first_factor = cl_true;
153         var cl_LF product;
154         var uintL b1;
155         var uintL b2;
156         for (b1 = 0, b2 = 1; b1 < lq; b1 = b2, b2 = ceiling(b2*23,8)) {
157                 // Piece containing bits b1+1..b2 after "decimal point"
158                 // in the binary representation of (p/2^lq).
159                 var uintL lqk = (lq >= b2 ? b2 : lq);
160                 var cl_I pk = ldb(p,cl_byte(lqk-b1,lq-lqk));
161                 // Compute exp(pk/2^lqk).
162                 if (!zerop(pk)) {
163                         if (minusp(x_.sign)) { pk = -pk; }
164                         var cl_LF factor = cl_exp_aux(pk,lqk,len);
165                         if (first_factor) {
166                                 product = factor;
167                                 first_factor = cl_false;
168                         } else
169                                 product = product * factor;
170                 }
171         }
172         if (first_factor)
173                 return cl_I_to_LF(1,len);
174         else
175                 return product;
176 }
177 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
178
179 // Timings of the above algorithms, on an i486 33 MHz, running Linux,
180 // applied to x = sqrt(2)-1 = 0.414...
181 // ("naive" with adaptive limit_slope, about sqrt(ln(len)).)
182 //   N      naive  ratseries
183 //   10     0.010   0.027
184 //   25     0.039   0.072
185 //   50     0.15    0.19
186 //  100     0.60    0.55
187 //  250     3.9     2.6
188 //  500    16.3     9.3
189 // 1000    68      29
190 // ==> ratseries faster for N >= 84.