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