1 /* origin: FreeBSD /usr/src/lib/msun/src/e_jn.c */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
6 * Developed at SunSoft, 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 * floating point Bessel's function of the 1st and 2nd kind
18 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
19 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
20 * Note 2. About jn(n,x), yn(n,x)
21 * For n=0, j0(x) is called,
22 * for n=1, j1(x) is called,
23 * for n<=x, forward recursion is used starting
24 * from values of j0(x) and j1(x).
25 * for n>x, a continued fraction approximation to
26 * j(n,x)/j(n-1,x) is evaluated and then backward
27 * recursion is used starting from a supposed value
28 * for j(n,x). The resulting value of j(0,x) is
29 * compared with the actual value to correct the
30 * supposed value of j(n,x).
32 * yn(n,x) is similar in all respects, except
33 * that forward recursion is used for all
37 use super::{cos, fabs, get_high_word, get_low_word, j0, j1, log, sin, sqrt, y0, y1}
;
39 const INVSQRTPI
: f64 = 5.64189583547756279280e-01; /* 0x3FE20DD7, 0x50429B6D */
41 pub fn jn(n
: i32, mut x
: f64) -> f64 {
51 ix
= get_high_word(x
);
53 sign
= (ix
>> 31) != 0;
57 if (ix
| (lx
| ((!lx
).wrapping_add(1))) >> 31) > 0x7ff00000 {
62 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
63 * Thus, J(-n,x) = J(n,-x)
65 /* nm1 = |n|-1 is used instead of |n| to handle n==INT_MIN */
80 sign
&= (n
& 1) != 0; /* even n: 0, odd n: signbit(x) */
82 if (ix
| lx
) == 0 || ix
== 0x7ff00000 {
83 /* if x is 0 or inf */
85 } else if (nm1
as f64) < x
{
86 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
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
95 * n sin(xn)*sqt2 cos(xn)*sqt2
96 * ----------------------------------
102 temp
= match nm1
& 3 {
103 0 => -cos(x
) + sin(x
),
104 1 => -cos(x
) - sin(x
),
105 2 => cos(x
) - sin(x
),
106 3 | _
=> cos(x
) + sin(x
),
108 b
= INVSQRTPI
* temp
/ sqrt(x
);
116 b
= b
* (2.0 * (i
as f64) / x
) - a
; /* avoid underflow */
123 /* x is tiny, return the first Taylor expansion of J(n,x)
124 * J(n,x) = 1/n!*(x/2)^n - ...
135 a
*= i
as f64; /* a = n! */
136 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
182 nf
= (nm1
as f64) + 1.0;
199 t
= 1.0 / (2.0 * ((i
as f64) + nf
) / x
- t
);
204 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
205 * Hence, if n*(log(2n/x)) > ...
206 * single 8.8722839355e+01
207 * double 7.09782712893383973096e+02
208 * long double 1.1356523406294143949491931077970765006170e+04
209 * then recurrent value may overflow and the result is
210 * likely underflow to zero
212 tmp
= nf
* log(fabs(w
));
213 if tmp
< 7.09782712893383973096e+02 {
217 b
= b
* (2.0 * (i
as f64)) / x
- a
;
225 b
= b
* (2.0 * (i
as f64)) / x
- a
;
227 /* scale b to avoid spurious overflow */
228 let x1p500
= f64::from_bits(0x5f30000000000000); // 0x1p500 == 2^500
239 if fabs(z
) >= fabs(w
) {
254 pub fn yn(n
: i32, x
: f64) -> f64 {
265 ix
= get_high_word(x
);
266 lx
= get_low_word(x
);
267 sign
= (ix
>> 31) != 0;
271 if (ix
| (lx
| ((!lx
).wrapping_add(1))) >> 31) > 0x7ff00000 {
275 if sign
&& (ix
| lx
) != 0 {
279 if ix
== 0x7ff00000 {
301 if ix
>= 0x52d00000 {
304 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
305 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
306 * Let s=sin(x), c=cos(x),
307 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
309 * n sin(xn)*sqt2 cos(xn)*sqt2
310 * ----------------------------------
316 temp
= match nm1
& 3 {
317 0 => -sin(x
) - cos(x
),
318 1 => -sin(x
) + cos(x
),
319 2 => sin(x
) + cos(x
),
320 3 | _
=> sin(x
) - cos(x
),
322 b
= INVSQRTPI
* temp
/ sqrt(x
);
326 /* quit if b is -inf */
327 ib
= get_high_word(b
);
329 while i
< nm1
&& ib
!= 0xfff00000 {
332 b
= (2.0 * (i
as f64) / x
) * b
- a
;
333 ib
= get_high_word(b
);