Fix sin, sincos missing underflows (bug 16526, bug 16538).
[glibc.git] / sysdeps / ieee754 / dbl-64 / e_jn.c
blob900737c401d51c9b8594d1653a1273440834eaa1
1 /* @(#)e_jn.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
14 * __ieee754_jn(n, x), __ieee754_yn(n, x)
15 * floating point Bessel's function of the 1st and 2nd kind
16 * of order n
18 * Special cases:
19 * y0(0)=y1(0)=yn(n,0) = -inf with overflow signal;
20 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
21 * Note 2. About jn(n,x), yn(n,x)
22 * For n=0, j0(x) is called,
23 * for n=1, j1(x) is called,
24 * for n<x, forward recursion us used starting
25 * from values of j0(x) and j1(x).
26 * for n>x, a continued fraction approximation to
27 * j(n,x)/j(n-1,x) is evaluated and then backward
28 * recursion is used starting from a supposed value
29 * for j(n,x). The resulting value of j(0,x) is
30 * compared with the actual value to correct the
31 * supposed value of j(n,x).
33 * yn(n,x) is similar in all respects, except
34 * that forward recursion is used for all
35 * values of n>1.
39 #include <errno.h>
40 #include <float.h>
41 #include <math.h>
42 #include <math_private.h>
44 static const double
45 invsqrtpi = 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
46 two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
47 one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
49 static const double zero = 0.00000000000000000000e+00;
51 double
52 __ieee754_jn (int n, double x)
54 int32_t i, hx, ix, lx, sgn;
55 double a, b, temp, di;
56 double z, w;
58 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
59 * Thus, J(-n,x) = J(n,-x)
61 EXTRACT_WORDS (hx, lx, x);
62 ix = 0x7fffffff & hx;
63 /* if J(n,NaN) is NaN */
64 if (__glibc_unlikely ((ix | ((u_int32_t) (lx | -lx)) >> 31) > 0x7ff00000))
65 return x + x;
66 if (n < 0)
68 n = -n;
69 x = -x;
70 hx ^= 0x80000000;
72 if (n == 0)
73 return (__ieee754_j0 (x));
74 if (n == 1)
75 return (__ieee754_j1 (x));
76 sgn = (n & 1) & (hx >> 31); /* even n -- 0, odd n -- sign(x) */
77 x = fabs (x);
78 if (__glibc_unlikely ((ix | lx) == 0 || ix >= 0x7ff00000))
79 /* if x is 0 or inf */
80 b = zero;
81 else if ((double) n <= x)
83 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
84 if (ix >= 0x52D00000) /* x > 2**302 */
85 { /* (x >> n**2)
86 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
87 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
88 * Let s=sin(x), c=cos(x),
89 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
91 * n sin(xn)*sqt2 cos(xn)*sqt2
92 * ----------------------------------
93 * 0 s-c c+s
94 * 1 -s-c -c+s
95 * 2 -s+c -c-s
96 * 3 s+c c-s
98 double s;
99 double c;
100 __sincos (x, &s, &c);
101 switch (n & 3)
103 case 0: temp = c + s; break;
104 case 1: temp = -c + s; break;
105 case 2: temp = -c - s; break;
106 case 3: temp = c - s; break;
108 b = invsqrtpi * temp / __ieee754_sqrt (x);
110 else
112 a = __ieee754_j0 (x);
113 b = __ieee754_j1 (x);
114 for (i = 1; i < n; i++)
116 temp = b;
117 b = b * ((double) (i + i) / x) - a; /* avoid underflow */
118 a = temp;
122 else
124 if (ix < 0x3e100000) /* x < 2**-29 */
125 { /* x is tiny, return the first Taylor expansion of J(n,x)
126 * J(n,x) = 1/n!*(x/2)^n - ...
128 if (n > 33) /* underflow */
129 b = zero;
130 else
132 temp = x * 0.5; b = temp;
133 for (a = one, i = 2; i <= n; i++)
135 a *= (double) i; /* a = n! */
136 b *= temp; /* b = (x/2)^n */
138 b = b / a;
141 else
143 /* use backward recurrence */
144 /* x x^2 x^2
145 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
146 * 2n - 2(n+1) - 2(n+2)
148 * 1 1 1
149 * (for large x) = ---- ------ ------ .....
150 * 2n 2(n+1) 2(n+2)
151 * -- - ------ - ------ -
152 * x x x
154 * Let w = 2n/x and h=2/x, then the above quotient
155 * is equal to the continued fraction:
157 * = -----------------------
159 * w - -----------------
161 * w+h - ---------
162 * w+2h - ...
164 * To determine how many terms needed, let
165 * Q(0) = w, Q(1) = w(w+h) - 1,
166 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
167 * When Q(k) > 1e4 good for single
168 * When Q(k) > 1e9 good for double
169 * When Q(k) > 1e17 good for quadruple
171 /* determine k */
172 double t, v;
173 double q0, q1, h, tmp; int32_t k, m;
174 w = (n + n) / (double) x; h = 2.0 / (double) x;
175 q0 = w; z = w + h; q1 = w * z - 1.0; k = 1;
176 while (q1 < 1.0e9)
178 k += 1; z += h;
179 tmp = z * q1 - q0;
180 q0 = q1;
181 q1 = tmp;
183 m = n + n;
184 for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
185 t = one / (i / x - t);
186 a = t;
187 b = one;
188 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
189 * Hence, if n*(log(2n/x)) > ...
190 * single 8.8722839355e+01
191 * double 7.09782712893383973096e+02
192 * long double 1.1356523406294143949491931077970765006170e+04
193 * then recurrent value may overflow and the result is
194 * likely underflow to zero
196 tmp = n;
197 v = two / x;
198 tmp = tmp * __ieee754_log (fabs (v * tmp));
199 if (tmp < 7.09782712893383973096e+02)
201 for (i = n - 1, di = (double) (i + i); i > 0; i--)
203 temp = b;
204 b *= di;
205 b = b / x - a;
206 a = temp;
207 di -= two;
210 else
212 for (i = n - 1, di = (double) (i + i); i > 0; i--)
214 temp = b;
215 b *= di;
216 b = b / x - a;
217 a = temp;
218 di -= two;
219 /* scale b to avoid spurious overflow */
220 if (b > 1e100)
222 a /= b;
223 t /= b;
224 b = one;
228 /* j0() and j1() suffer enormous loss of precision at and
229 * near zero; however, we know that their zero points never
230 * coincide, so just choose the one further away from zero.
232 z = __ieee754_j0 (x);
233 w = __ieee754_j1 (x);
234 if (fabs (z) >= fabs (w))
235 b = (t * z / b);
236 else
237 b = (t * w / a);
240 if (sgn == 1)
241 return -b;
242 else
243 return b;
245 strong_alias (__ieee754_jn, __jn_finite)
247 double
248 __ieee754_yn (int n, double x)
250 int32_t i, hx, ix, lx;
251 int32_t sign;
252 double a, b, temp, ret;
254 EXTRACT_WORDS (hx, lx, x);
255 ix = 0x7fffffff & hx;
256 /* if Y(n,NaN) is NaN */
257 if (__glibc_unlikely ((ix | ((u_int32_t) (lx | -lx)) >> 31) > 0x7ff00000))
258 return x + x;
259 if (__glibc_unlikely ((ix | lx) == 0))
260 return -HUGE_VAL + x;
261 /* -inf and overflow exception. */;
262 if (__glibc_unlikely (hx < 0))
263 return zero / (zero * x);
264 sign = 1;
265 if (n < 0)
267 n = -n;
268 sign = 1 - ((n & 1) << 1);
270 if (n == 0)
271 return (__ieee754_y0 (x));
273 SET_RESTORE_ROUND (FE_TONEAREST);
274 if (n == 1)
276 ret = sign * __ieee754_y1 (x);
277 goto out;
279 if (__glibc_unlikely (ix == 0x7ff00000))
280 return zero;
281 if (ix >= 0x52D00000) /* x > 2**302 */
282 { /* (x >> n**2)
283 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
284 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
285 * Let s=sin(x), c=cos(x),
286 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
288 * n sin(xn)*sqt2 cos(xn)*sqt2
289 * ----------------------------------
290 * 0 s-c c+s
291 * 1 -s-c -c+s
292 * 2 -s+c -c-s
293 * 3 s+c c-s
295 double c;
296 double s;
297 __sincos (x, &s, &c);
298 switch (n & 3)
300 case 0: temp = s - c; break;
301 case 1: temp = -s - c; break;
302 case 2: temp = -s + c; break;
303 case 3: temp = s + c; break;
305 b = invsqrtpi * temp / __ieee754_sqrt (x);
307 else
309 u_int32_t high;
310 a = __ieee754_y0 (x);
311 b = __ieee754_y1 (x);
312 /* quit if b is -inf */
313 GET_HIGH_WORD (high, b);
314 for (i = 1; i < n && high != 0xfff00000; i++)
316 temp = b;
317 b = ((double) (i + i) / x) * b - a;
318 GET_HIGH_WORD (high, b);
319 a = temp;
321 /* If B is +-Inf, set up errno accordingly. */
322 if (!isfinite (b))
323 __set_errno (ERANGE);
325 if (sign > 0)
326 ret = b;
327 else
328 ret = -b;
330 out:
331 if (isinf (ret))
332 ret = __copysign (DBL_MAX, ret) * DBL_MAX;
333 return ret;
335 strong_alias (__ieee754_yn, __yn_finite)