]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/ieee754/dbl-64/e_jn.c
f48e43a0d998a15dd16badff462a6ce1d31ebc28
1 /* @(#)e_jn.c 5.1 93/09/24 */
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
10 * ====================================================
14 * __ieee754_jn(n, x), __ieee754_yn(n, x)
15 * floating point Bessel's function of the 1st and 2nd kind
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
41 #include <math_private.h>
44 invsqrtpi
= 5.64189583547756279280e-01, /* 0x3FE20DD7, 0x50429B6D */
45 two
= 2.00000000000000000000e+00, /* 0x40000000, 0x00000000 */
46 one
= 1.00000000000000000000e+00; /* 0x3FF00000, 0x00000000 */
48 static const double zero
= 0.00000000000000000000e+00;
51 __ieee754_jn (int n
, double x
)
53 int32_t i
, hx
, ix
, lx
, sgn
;
54 double a
, b
, temp
, di
;
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)
60 EXTRACT_WORDS (hx
, lx
, x
);
62 /* if J(n,NaN) is NaN */
63 if (__builtin_expect ((ix
| ((u_int32_t
) (lx
| -lx
)) >> 31) > 0x7ff00000, 0))
72 return (__ieee754_j0 (x
));
74 return (__ieee754_j1 (x
));
75 sgn
= (n
& 1) & (hx
>> 31); /* even n -- 0, odd n -- sign(x) */
77 if (__builtin_expect ((ix
| lx
) == 0 || ix
>= 0x7ff00000, 0))
78 /* if x is 0 or inf */
80 else if ((double) n
<= x
)
82 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
83 if (ix
>= 0x52D00000) /* x > 2**302 */
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
90 * n sin(xn)*sqt2 cos(xn)*sqt2
91 * ----------------------------------
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;
107 b
= invsqrtpi
* temp
/ __ieee754_sqrt (x
);
111 a
= __ieee754_j0 (x
);
112 b
= __ieee754_j1 (x
);
113 for (i
= 1; i
< n
; i
++)
116 b
= b
* ((double) (i
+ i
) / x
) - a
; /* avoid underflow */
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 - ...
127 if (n
> 33) /* underflow */
131 temp
= x
* 0.5; b
= temp
;
132 for (a
= one
, i
= 2; i
<= n
; i
++)
134 a
*= (double) i
; /* a = n! */
135 b
*= temp
; /* b = (x/2)^n */
142 /* use backward recurrence */
144 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
145 * 2n - 2(n+1) - 2(n+2)
148 * (for large x) = ---- ------ ------ .....
150 * -- - ------ - ------ -
153 * Let w = 2n/x and h=2/x, then the above quotient
154 * is equal to the continued fraction:
156 * = -----------------------
158 * w - -----------------
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
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;
183 for (t
= zero
, i
= 2 * (n
+ k
); i
>= m
; i
-= 2)
184 t
= one
/ (i
/ x
- t
);
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
197 tmp
= tmp
* __ieee754_log (fabs (v
* tmp
));
198 if (tmp
< 7.09782712893383973096e+02)
200 for (i
= n
- 1, di
= (double) (i
+ i
); i
> 0; i
--)
211 for (i
= n
- 1, di
= (double) (i
+ i
); i
> 0; i
--)
218 /* scale b to avoid spurious overflow */
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.
231 z
= __ieee754_j0 (x
);
232 w
= __ieee754_j1 (x
);
233 if (fabs (z
) >= fabs (w
))
244 strong_alias (__ieee754_jn
, __jn_finite
)
247 __ieee754_yn (int n
, double x
)
249 int32_t i
, hx
, ix
, lx
;
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))
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
);
267 sign
= 1 - ((n
& 1) << 1);
270 return (__ieee754_y0 (x
));
272 return (sign
* __ieee754_y1 (x
));
273 if (__builtin_expect (ix
== 0x7ff00000, 0))
275 if (ix
>= 0x52D00000) /* x > 2**302 */
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
282 * n sin(xn)*sqt2 cos(xn)*sqt2
283 * ----------------------------------
291 __sincos (x
, &s
, &c
);
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;
299 b
= invsqrtpi
* temp
/ __ieee754_sqrt (x
);
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
++)
311 b
= ((double) (i
+ i
) / x
) * b
- a
;
312 GET_HIGH_WORD (high
, b
);
315 /* If B is +-Inf, set up errno accordingly. */
317 __set_errno (ERANGE
);
324 strong_alias (__ieee754_yn
, __yn_finite
)