1/* @(#)e_jn.c 5.1 93/09/24 */
2/*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
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 * ====================================================
11 */
12
13/*
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
17 *
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).
32 *
33 * yn(n,x) is similar in all respects, except
34 * that forward recursion is used for all
35 * values of n>1.
36 *
37 */
38
39#include <errno.h>
40#include <float.h>
41#include <math.h>
42#include <math-narrow-eval.h>
43#include <math_private.h>
44#include <math-underflow.h>
45
46static const double
47 invsqrtpi = 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
48 two = 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
49 one = 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
50
51static const double zero = 0.00000000000000000000e+00;
52
53double
54__ieee754_jn (int n, double x)
55{
56 int32_t i, hx, ix, lx, sgn;
57 double a, b, temp, di, ret;
58 double z, w;
59
60 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
61 * Thus, J(-n,x) = J(n,-x)
62 */
63 EXTRACT_WORDS (hx, lx, x);
64 ix = 0x7fffffff & hx;
65 /* if J(n,NaN) is NaN */
66 if (__glibc_unlikely ((ix | ((uint32_t) (lx | -lx)) >> 31) > 0x7ff00000))
67 return x + x;
68 if (n < 0)
69 {
70 n = -n;
71 x = -x;
72 hx ^= 0x80000000;
73 }
74 if (n == 0)
75 return (__ieee754_j0 (x));
76 if (n == 1)
77 return (__ieee754_j1 (x));
78 sgn = (n & 1) & (hx >> 31); /* even n -- 0, odd n -- sign(x) */
79 x = fabs (x);
80 {
81 SET_RESTORE_ROUND (FE_TONEAREST);
82 if (__glibc_unlikely ((ix | lx) == 0 || ix >= 0x7ff00000))
83 /* if x is 0 or inf */
84 return sgn == 1 ? -zero : zero;
85 else if ((double) n <= x)
86 {
87 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
88 if (ix >= 0x52D00000) /* x > 2**302 */
89 { /* (x >> n**2)
90 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
91 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
92 * Let s=sin(x), c=cos(x),
93 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
94 *
95 * n sin(xn)*sqt2 cos(xn)*sqt2
96 * ----------------------------------
97 * 0 s-c c+s
98 * 1 -s-c -c+s
99 * 2 -s+c -c-s
100 * 3 s+c c-s
101 */
102 double s;
103 double c;
104 __sincos (x, &s, &c);
105 switch (n & 3)
106 {
107 case 0: temp = c + s; break;
108 case 1: temp = -c + s; break;
109 case 2: temp = -c - s; break;
110 case 3: temp = c - s; break;
111 }
112 b = invsqrtpi * temp / sqrt (x);
113 }
114 else
115 {
116 a = __ieee754_j0 (x);
117 b = __ieee754_j1 (x);
118 for (i = 1; i < n; i++)
119 {
120 temp = b;
121 b = b * ((double) (i + i) / x) - a; /* avoid underflow */
122 a = temp;
123 }
124 }
125 }
126 else
127 {
128 if (ix < 0x3e100000) /* x < 2**-29 */
129 { /* x is tiny, return the first Taylor expansion of J(n,x)
130 * J(n,x) = 1/n!*(x/2)^n - ...
131 */
132 if (n > 33) /* underflow */
133 b = zero;
134 else
135 {
136 temp = x * 0.5; b = temp;
137 for (a = one, i = 2; i <= n; i++)
138 {
139 a *= (double) i; /* a = n! */
140 b *= temp; /* b = (x/2)^n */
141 }
142 b = b / a;
143 }
144 }
145 else
146 {
147 /* use backward recurrence */
148 /* x x^2 x^2
149 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
150 * 2n - 2(n+1) - 2(n+2)
151 *
152 * 1 1 1
153 * (for large x) = ---- ------ ------ .....
154 * 2n 2(n+1) 2(n+2)
155 * -- - ------ - ------ -
156 * x x x
157 *
158 * Let w = 2n/x and h=2/x, then the above quotient
159 * is equal to the continued fraction:
160 * 1
161 * = -----------------------
162 * 1
163 * w - -----------------
164 * 1
165 * w+h - ---------
166 * w+2h - ...
167 *
168 * To determine how many terms needed, let
169 * Q(0) = w, Q(1) = w(w+h) - 1,
170 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
171 * When Q(k) > 1e4 good for single
172 * When Q(k) > 1e9 good for double
173 * When Q(k) > 1e17 good for quadruple
174 */
175 /* determine k */
176 double t, v;
177 double q0, q1, h, tmp; int32_t k, m;
178 w = (n + n) / (double) x; h = 2.0 / (double) x;
179 q0 = w; z = w + h; q1 = w * z - 1.0; k = 1;
180 while (q1 < 1.0e9)
181 {
182 k += 1; z += h;
183 tmp = z * q1 - q0;
184 q0 = q1;
185 q1 = tmp;
186 }
187 m = n + n;
188 for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
189 t = one / (i / x - t);
190 a = t;
191 b = one;
192 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
193 * Hence, if n*(log(2n/x)) > ...
194 * single 8.8722839355e+01
195 * double 7.09782712893383973096e+02
196 * long double 1.1356523406294143949491931077970765006170e+04
197 * then recurrent value may overflow and the result is
198 * likely underflow to zero
199 */
200 tmp = n;
201 v = two / x;
202 tmp = tmp * __ieee754_log (fabs (v * tmp));
203 if (tmp < 7.09782712893383973096e+02)
204 {
205 for (i = n - 1, di = (double) (i + i); i > 0; i--)
206 {
207 temp = b;
208 b *= di;
209 b = b / x - a;
210 a = temp;
211 di -= two;
212 }
213 }
214 else
215 {
216 for (i = n - 1, di = (double) (i + i); i > 0; i--)
217 {
218 temp = b;
219 b *= di;
220 b = b / x - a;
221 a = temp;
222 di -= two;
223 /* scale b to avoid spurious overflow */
224 if (b > 1e100)
225 {
226 a /= b;
227 t /= b;
228 b = one;
229 }
230 }
231 }
232 /* j0() and j1() suffer enormous loss of precision at and
233 * near zero; however, we know that their zero points never
234 * coincide, so just choose the one further away from zero.
235 */
236 z = __ieee754_j0 (x);
237 w = __ieee754_j1 (x);
238 if (fabs (z) >= fabs (w))
239 b = (t * z / b);
240 else
241 b = (t * w / a);
242 }
243 }
244 if (sgn == 1)
245 ret = -b;
246 else
247 ret = b;
248 ret = math_narrow_eval (ret);
249 }
250 if (ret == 0)
251 {
252 ret = math_narrow_eval (__copysign (DBL_MIN, ret) * DBL_MIN);
253 __set_errno (ERANGE);
254 }
255 else
256 math_check_force_underflow (ret);
257 return ret;
258}
259strong_alias (__ieee754_jn, __jn_finite)
260
261double
262__ieee754_yn (int n, double x)
263{
264 int32_t i, hx, ix, lx;
265 int32_t sign;
266 double a, b, temp, ret;
267
268 EXTRACT_WORDS (hx, lx, x);
269 ix = 0x7fffffff & hx;
270 /* if Y(n,NaN) is NaN */
271 if (__glibc_unlikely ((ix | ((uint32_t) (lx | -lx)) >> 31) > 0x7ff00000))
272 return x + x;
273 sign = 1;
274 if (n < 0)
275 {
276 n = -n;
277 sign = 1 - ((n & 1) << 1);
278 }
279 if (n == 0)
280 return (__ieee754_y0 (x));
281 if (__glibc_unlikely ((ix | lx) == 0))
282 return -sign / zero;
283 /* -inf and overflow exception. */;
284 if (__glibc_unlikely (hx < 0))
285 return zero / (zero * x);
286 {
287 SET_RESTORE_ROUND (FE_TONEAREST);
288 if (n == 1)
289 {
290 ret = sign * __ieee754_y1 (x);
291 goto out;
292 }
293 if (__glibc_unlikely (ix == 0x7ff00000))
294 return zero;
295 if (ix >= 0x52D00000) /* x > 2**302 */
296 { /* (x >> n**2)
297 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
298 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
299 * Let s=sin(x), c=cos(x),
300 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
301 *
302 * n sin(xn)*sqt2 cos(xn)*sqt2
303 * ----------------------------------
304 * 0 s-c c+s
305 * 1 -s-c -c+s
306 * 2 -s+c -c-s
307 * 3 s+c c-s
308 */
309 double c;
310 double s;
311 __sincos (x, &s, &c);
312 switch (n & 3)
313 {
314 case 0: temp = s - c; break;
315 case 1: temp = -s - c; break;
316 case 2: temp = -s + c; break;
317 case 3: temp = s + c; break;
318 }
319 b = invsqrtpi * temp / sqrt (x);
320 }
321 else
322 {
323 uint32_t high;
324 a = __ieee754_y0 (x);
325 b = __ieee754_y1 (x);
326 /* quit if b is -inf */
327 GET_HIGH_WORD (high, b);
328 for (i = 1; i < n && high != 0xfff00000; i++)
329 {
330 temp = b;
331 b = ((double) (i + i) / x) * b - a;
332 GET_HIGH_WORD (high, b);
333 a = temp;
334 }
335 /* If B is +-Inf, set up errno accordingly. */
336 if (!isfinite (b))
337 __set_errno (ERANGE);
338 }
339 if (sign > 0)
340 ret = b;
341 else
342 ret = -b;
343 }
344 out:
345 if (isinf (ret))
346 ret = __copysign (DBL_MAX, ret) * DBL_MAX;
347 return ret;
348}
349strong_alias (__ieee754_yn, __yn_finite)
350