]> www.ginac.de Git - cln.git/blob - src/float/transcendental/cl_F_atanhx.cc
Initial revision
[cln.git] / src / float / transcendental / cl_F_atanhx.cc
1 // atanhx().
2
3 // General includes.
4 #include "cl_sysdep.h"
5
6 // Specification.
7 #include "cl_F_tran.h"
8 #include "cl_F.h"
9 #include "cl_lfloat.h"
10 #include "cl_LF.h"
11
12
13 // Implementation.
14
15 #include "cl_float.h"
16 #include "cl_low.h"
17
18 #undef MAYBE_INLINE
19 #define MAYBE_INLINE inline
20 #include "cl_LF_zerop.cc"
21 #include "cl_LF_minusp.cc"
22 #include "cl_LF_exponent.cc"
23
24 // cl_F atanhx (const cl_F& x)
25 // cl_LF atanhx (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/2 liefere x
30 //   (denn bei e<=-d/2 ist x^2 < 2^(-d), also
31 //   1 <= atanh(x)/x = 1+x^2/3+x^4/5+... < 1+x^2/2 < 1+2^(-d-1) < 1+2^(-d),
32 //   also ist atanh(x)/x, auf d Bits gerundet, gleich 1.0).
33 // Bei großem d verwende die Formel ln((1+x)/(1-x))/2 (asymptotisch schneller),
34 //   aber erhöhe die Genauigkeit, so daß beim Bilden von 1+x keine Bits verloren
35 //   gehen.
36 // Bei e<=-sqrt(d) verwende die Potenzreihe
37 //   atanh(x)/x = sum(j=0..inf,(x^2)^j/(2j+1)):
38 //   a:=x^2, b:=1, i:=1, sum:=0,
39 //   while (/= sum (setq sum (+ sum (/ b i)))) do i:=i+2, b:=b*a.
40 //   Ergebnis x*sum.
41 // Sonst setze y := x/(1+sqrt(1-x^2)), berechne rekursiv z:=atanh(y)
42 //   und liefere 2*z = (scale-float z 1).
43 // Diese Rekursion wird entrekursiviert. Statt k mal hintereinander
44 //   x := x/(1+sqrt(1-x^2)) zu bilden, arbeitet man lieber mit den Kehrwerten,
45 //   setzt also x := 1/|x|, dann k mal x := x+sqrt(x^2-1), dann x := +- 1/x.
46 // Aufwand: asymptotisch d^2.5 .
47
48 const cl_LF atanhx (const cl_LF& x)
49 {
50         if (zerop(x))
51                 return x;
52         var uintL actuallen = TheLfloat(x)->len;
53         var uintL d = float_digits(x);
54         var sintL e = float_exponent(x);
55         if (e <= (sintL)(-d)>>1) // e <= -d/2 <==> e <= -ceiling(d/2)
56                 return x; // ja -> x als Ergebnis
57         if (actuallen >= 34) {
58                 DeclareType(cl_LF,x);
59                 var cl_LF xx = extend(x,TheLfloat(x)->len+ceiling((uintL)(-e),intDsize));
60                 return cl_float(scale_float(ln((1+xx)/(1-xx)),-1),x);
61         }
62         var uintL k = 0; // Rekursionszähler k:=0
63         // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
64         // angewandt werden. limit_slope = 1.0 ist schlecht (ca. 15% zu
65         // schlecht). Ein guter Wert ist:
66         // für naive1: limit_scope = 0.625 = 5/8,
67         // für naive2: limit_scope = 0.4 = 13/32.
68         var uintL sqrt_d = floor(isqrt(d)*13,32); // limit_slope*floor(sqrt(d))
69         var cl_LF xx = x;
70         if (e >= (sintL)(-sqrt_d)) {
71                 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
72                 var sintL e_limit = 1+sqrt_d; // 1+limit_slope*floor(sqrt(d))
73                 xx = recip(abs(xx)); // 1/|x|
74                 do {
75                   // nächstes x nach der Formel x := x+sqrt(x^2 - 1) berechnen:
76                   xx = sqrt(square(xx) + cl_float(-1,xx)) + xx;
77                   k = k+1;
78                 } until (float_exponent(xx) > e_limit);
79                 // Schleifenende mit Exponent(x) > 1+limit_slope*floor(sqrt(d)),
80                 // also x >= 2^(1+limit_slope*floor(sqrt(d))),
81                 // also 1/x <= 2^(-1-limit_slope*floor(sqrt(d))).
82                 // Nun kann die Potenzreihe auf 1/x angewandt werden.
83                 xx = recip(xx);
84                 if (minusp(x))
85                         xx = - xx; // Vorzeichen wieder rein
86         }
87         // Potenzreihe anwenden:
88         var int i = 1;
89         var cl_LF a = square(xx); // a = x^2
90         var cl_LF b = cl_float(1,xx); // b := (float 1 x)
91         var cl_LF sum = cl_float(0,xx); // sum := (float 0 x)
92         if (0) {
93                 // naive1:
94                 // floating-point representation
95                 loop {
96                         var cl_LF new_sum = sum + b/(cl_I)i; // (+ sum (/ b i))
97                         if (new_sum == sum) // = sum ?
98                                 break; // ja -> Potenzreihe abbrechen
99                         sum = new_sum;
100                         b = b*a;
101                         i = i+2;
102                 }
103         } else {
104                 // naive2:
105                 // floating-point representation with smooth precision reduction
106                 var cl_LF eps = scale_float(b,-(sintL)d-10);
107                 loop {
108                         var cl_LF new_sum = sum + LF_to_LF(b/(cl_I)i,actuallen); // (+ sum (/ b i))
109                         if (new_sum == sum) // = sum ?
110                                 break; // ja -> Potenzreihe abbrechen
111                         sum = new_sum;
112                         b = cl_LF_shortenwith(b,eps);
113                         b = b*a;
114                         i = i+2;
115                 }
116         }
117         var cl_LF erg = sum*xx; // sum*x als Ergebnis
118         return scale_float(erg,k); // wegen Rekursion noch mal 2^k
119 }
120 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
121
122 const cl_F atanhx (const cl_F& x)
123 {
124         if (longfloatp(x)) {
125                 DeclareType(cl_LF,x);
126                 return atanhx(x);
127         }
128         if (zerop(x))
129                 return x;
130         var uintL d = float_digits(x);
131         var sintL e = float_exponent(x);
132         if (e <= (sintL)(-d)>>1) // e <= -d/2 <==> e <= -ceiling(d/2)
133                 return x; // ja -> x als Ergebnis
134         var uintL k = 0; // Rekursionszähler k:=0
135         // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
136         // angewandt werden. limit_slope = 1.0 ist schlecht (ca. 15% zu
137         // schlecht). Ein guter Wert ist limit_scope = 0.625 = 5/8.
138         var uintL sqrt_d = floor(isqrt(d)*5,8); // limit_slope*floor(sqrt(d))
139         var cl_F xx = x;
140         if (e >= (sintL)(-sqrt_d)) {
141                 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
142                 var sintL e_limit = 1+sqrt_d; // 1+limit_slope*floor(sqrt(d))
143                 xx = recip(abs(xx)); // 1/|x|
144                 do {
145                   // nächstes x nach der Formel x := x+sqrt(x^2 - 1) berechnen:
146                   xx = sqrt(square(xx) + cl_float(-1,xx)) + xx;
147                   k = k+1;
148                 } until (float_exponent(xx) > e_limit);
149                 // Schleifenende mit Exponent(x) > 1+limit_slope*floor(sqrt(d)),
150                 // also x >= 2^(1+limit_slope*floor(sqrt(d))),
151                 // also 1/x <= 2^(-1-limit_slope*floor(sqrt(d))).
152                 // Nun kann die Potenzreihe auf 1/x angewandt werden.
153                 xx = recip(xx);
154                 if (minusp(x))
155                         xx = - xx; // Vorzeichen wieder rein
156         }
157         // Potenzreihe anwenden:
158         var int i = 1;
159         var cl_F a = square(xx); // a = x^2
160         var cl_F b = cl_float(1,xx); // b := (float 1 x)
161         var cl_F sum = cl_float(0,xx); // sum := (float 0 x)
162         loop {
163                 var cl_F new_sum = sum + b / (cl_I)i; // (+ sum (/ b i))
164                 if (new_sum == sum) // = sum ?
165                         break; // ja -> Potenzreihe abbrechen
166                 sum = new_sum;
167                 b = b*a;
168                 i = i+2;
169         }
170         var cl_F erg = sum*xx; // sum*x als Ergebnis
171         return scale_float(erg,k); // wegen Rekursion noch mal 2^k
172 }
173 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
174
175 // Timings of the above algorithms, on an i486 33 MHz, running Linux,
176 // applied to x = sqrt(2)-1 = 0.414...
177 //   N      naive1  naive2  use ln
178 //   10     0.013   0.013   0.015
179 //   25     0.064   0.050   0.049
180 //   50     0.25    0.018   0.17
181 //  100     1.07    0.75    0.64
182 //  250     7.6     5.2     2.7
183 //  500    35.5    24.2     9.7
184 // 1000   168     116      29.6
185 // ==> using ln faster for N >= 34.