19 #define MAYBE_INLINE inline
20 #include "cl_LF_zerop.cc"
21 #include "cl_LF_minusp.cc"
22 #include "cl_LF_exponent.cc"
24 // cl_F atanhx (const cl_F& x)
25 // cl_LF atanhx (const cl_LF& x)
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
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.
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 .
48 const cl_LF atanhx (const cl_LF& 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) {
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);
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))
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|
75 // nächstes x nach der Formel x := x+sqrt(x^2 - 1) berechnen:
76 xx = sqrt(square(xx) + cl_float(-1,xx)) + xx;
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.
85 xx = - xx; // Vorzeichen wieder rein
87 // Potenzreihe anwenden:
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)
94 // floating-point representation
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
105 // floating-point representation with smooth precision reduction
106 var cl_LF eps = scale_float(b,-(sintL)d-10);
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
112 b = cl_LF_shortenwith(b,eps);
117 var cl_LF erg = sum*xx; // sum*x als Ergebnis
118 return scale_float(erg,k); // wegen Rekursion noch mal 2^k
120 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
122 const cl_F atanhx (const cl_F& x)
125 DeclareType(cl_LF,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))
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|
145 // nächstes x nach der Formel x := x+sqrt(x^2 - 1) berechnen:
146 xx = sqrt(square(xx) + cl_float(-1,xx)) + xx;
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.
155 xx = - xx; // Vorzeichen wieder rein
157 // Potenzreihe anwenden:
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)
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
170 var cl_F erg = sum*xx; // sum*x als Ergebnis
171 return scale_float(erg,k); // wegen Rekursion noch mal 2^k
173 // Bit complexity (N = length(x)): O(log(N)^2*M(N)).
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
185 // ==> using ln faster for N >= 34.