1 // sinhxbyx(), sinhx().
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 // sinhxbyx is mainly for cl_SF, cl_FF, cl_DF, where we want to avoid underflow.
28 const cl_F sinhxbyx_naive (const cl_F& x)
31 // e := Exponent aus (decode-float x), d := (float-digits x)
32 // Bei x=0.0 oder e<=(1-d)/2 liefere 1.0
33 // (denn bei e<=(1-d)/2 ist x^2/6 < x^2/4 < 2^(1-d)/4 = 2^(-d-1), also
34 // 1 <= sinh(x)/x = 1+x^2/6+... < 1+2^(-d-1), also 1 <= (sinh(x)/x)^2 < 1+2^(-d),
35 // also ist (sinh(x)/x)^2, auf d Bits gerundet, gleich 1.0).
36 // Bei e<=-sqrt(d) verwende die Potenzreihe
37 // sinh(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))) do b:=b*a/((i+1)*(i+2)), i:=i+2.
41 // Sonst setze y := x/2 = (scale-float x -1),
42 // berechne rekursiv z:=(sinh(y)/y)^2 und liefere z*(1+y^2*z).
43 // [Die Grenze sqrt(d) ergibt sich so:
44 // Man braucht bei der Potenzreihe mit x=2^-k etwa j Glieder, mit
45 // k*j*ln 2 + j*(ln j - 1) = d, und der Aufwand beträgt etwa 2.8*(j/2)
46 // Multiplikationen von d-Bit-Zahlen. Bei Halbierungen bis x=2^-k ist der
47 // Gesamtaufwand etwa 2*(k+e)+1.4*j(k). Dieses minimieren nach k: Soll sein
48 // -1.4 = d/dk j(k) = (d/dj k(j))^-1 = - j^2/(d+j)*ln 2, also j^2=2(d+j),
49 // grob j=sqrt(2d) und damit k=sqrt(d).]
50 // Aufwand: asymptotisch d^2.5 .
54 var uintL d = float_digits(x);
55 var sintL e = float_exponent(x);
56 if (e <= (1-(sintL)d)>>1) // e <= (1-d)/2 <==> e <= -ceiling((d-1)/2) ?
57 return cl_float(1,x); // ja -> 1.0 als Ergebnis
59 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
60 // angewandt werden. Wähle limit_slope = 13/32 = 0.4.
61 var sintL e_limit = -1-floor(isqrt(d)*13,32); // -1-floor(sqrt(d))
63 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
64 x = scale_float(x,e_limit-e);
65 // Neuer Exponent = e_limit.
67 var cl_F x2 = square(x); // x^2
68 // Potenzreihe anwenden:
69 var cl_F a = x2; // a := x^2
71 var cl_F b = cl_float(1,x); // b := (float 1 x)
72 var cl_F sum = cl_float(0,x); // sum := (float 0 x)
74 var cl_F new_sum = sum + b;
75 if (new_sum == sum) // = sum ?
76 break; // ja -> Potenzreihe abbrechen
78 b = (b*a)/(cl_I)((i+1)*(i+2));
81 var cl_F z = square(sum); // sum^2 als Ergebnis
83 z = z + x2 * square(z);
84 x2 = scale_float(x2,2); // x^2 := x^2*4
89 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
91 const cl_LF sinhx_naive (const cl_LF& x)
94 // e := Exponent aus (decode-float x), d := (float-digits x)
95 // Bei x=0.0 oder e<=(1-d)/2 liefere x
96 // (denn bei e<=(1-d)/2 ist x^2/6 < x^2/4 < 2^(1-d)/4 = 2^(-d-1), also
97 // 1 <= sinh(x)/x = 1+x^2/6+... < 1+2^(-d-1), also ist sinh(x)^2, auf d Bits
98 // gerundet, gleich x).
99 // Bei e<=-sqrt(d) verwende die Potenzreihe
100 // sinh(x) = sum(j=0..inf,x*(x^2)^j/(2j+1)!):
101 // a:=x^2, b:=x, i:=1, sum:=0,
102 // while (/= sum (setq sum (+ sum b))) do b:=b*a/((i+1)*(i+2)), i:=i+2.
104 // Sonst setze y := x/2 = (scale-float x -1),
105 // berechne rekursiv z:=sinh(y)^2 und liefere 4*z*(1+z) = (1+2*z)^2-1.
106 // [Die Grenze sqrt(d) ergibt sich so:
107 // Man braucht bei der Potenzreihe mit x=2^-k etwa j Glieder, mit
108 // k*j*ln 2 + j*(ln j - 1) = d, und der Aufwand beträgt etwa 2.8*(j/2)
109 // Multiplikationen von d-Bit-Zahlen. Bei Halbierungen bis x=2^-k ist der
110 // Gesamtaufwand etwa 2*(k+e)+1.4*j(k). Dieses minimieren nach k: Soll sein
111 // -1.4 = d/dk j(k) = (d/dj k(j))^-1 = - j^2/(d+j)*ln 2, also j^2=2(d+j),
112 // grob j=sqrt(2d) und damit k=sqrt(d).]
113 // Aufwand: asymptotisch d^2.5 .
117 var uintL actuallen = TheLfloat(x)->len;
118 var uintL d = float_digits(x);
119 var sintL e = float_exponent(x);
120 if (e <= (1-(sintL)d)>>1) // e <= (1-d)/2 <==> e <= -ceiling((d-1)/2) ?
121 return x; // ja -> x als Ergebnis
124 // Bei e <= -1-limit_slope*floor(sqrt(d)) kann die Potenzreihe
125 // angewandt werden. Ein guter Wert für naive1 ist limit_slope = 0.6,
126 // für naive3 aber limit_slope = 0.5.
127 var sintL e_limit = -1-floor(isqrt(d),2); // -1-floor(sqrt(d))
129 // e > -1-limit_slope*floor(sqrt(d)) -> muß |x| verkleinern.
130 x = scale_float(x,e_limit-e);
131 ee = e_limit; // Neuer Exponent = e_limit.
133 var cl_LF x2 = square(x); // x^2
134 // Potenzreihe anwenden:
135 var cl_LF powser_value;
136 var cl_LF a = x2; // a := x^2
140 // fixed-point representation
141 d = d-ee; // fixed-point representation with d mantissa bits
142 var cl_I b = round1(scale_float(x,d)); // b := x
143 var cl_I sum = 0; // sum := (float 0 x)
147 b = round1(round1(The(cl_LF)(b*a)),(cl_I)((i+1)*(i+2)));
150 powser_value = scale_float(cl_float(sum,x),-d);
151 } else if (actuallen <= 7) { // Break-even-Point before extendsqrt: N<=6
153 // floating-point representation
154 var cl_LF b = x; // b := x
155 var cl_LF sum = cl_float(0,x); // sum := (float 0 x)
157 var cl_LF new_sum = sum + b;
158 if (new_sum == sum) // = sum ?
159 break; // ja -> Potenzreihe abbrechen
161 b = (b*a)/(cl_I)((i+1)*(i+2));
167 // floating-point representation with smooth precision reduction
168 var cl_LF b = x; // b := x
169 var cl_LF eps = scale_float(b,-(sintL)d-10);
170 var cl_LF sum = cl_float(0,x); // sum := (float 0 x)
172 var cl_LF new_sum = sum + LF_to_LF(b,actuallen);
173 if (new_sum == sum) // = sum ?
174 break; // ja -> Potenzreihe abbrechen
176 b = cl_LF_shortenwith(b,eps);
177 b = (b*a)/(cl_I)((i+1)*(i+2));
182 var cl_LF z = square(powser_value); // sinh^2 als Ergebnis
183 while (e > e_limit) {
184 z = square(cl_float(1,x) + scale_float(z,1)) - cl_float(1,x); // z := (1+2*z)^2-1
189 // Bit complexity (N = length(x)): O(N^(1/2)*M(N)).
191 // Timings of the three variants, on an i486 33 MHz, running Linux,
192 // applied to x = sqrt(2)-1 = 0.414...
193 // N naive1 naive2 naive3 ratseries exp&recip
194 // 4 0.0055 0.0039 0.0041 0.021 0.0046
195 // 6 0.0073 0.0054 0.0054 0.029 0.0062
196 // 8 0.0093 0.0075 0.0070 0.036 0.0081
197 // 10 0.011 0.010 0.009 0.046 0.0011
198 // 25 0.041 0.046 0.033 0.133 0.043
199 // 50 0.14 0.18 0.12 0.36 0.16
200 // 100 0.56 0.70 0.43 1.12 0.61
201 // 250 3.5 4.5 2.7 5.3 3.3
202 // 500 14.9 19.4 11.4 19.0 11.4
203 // 1000 63 82 47 63 35
204 // 2500 328 381 243 261 143
205 // ==> naive2 fastest for N <= 6,
206 // naive3 fastest for 6 <= N <= 500,
207 // exp&recip (which uses exp's own ratseries) fastest for N >= 500.