/rust/registry/src/index.crates.io-6f17d22bba15001f/libm-0.2.11/src/math/jnf.rs
Line | Count | Source (jump to first uncovered line) |
1 | | /* origin: FreeBSD /usr/src/lib/msun/src/e_jnf.c */ |
2 | | /* |
3 | | * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. |
4 | | */ |
5 | | /* |
6 | | * ==================================================== |
7 | | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
8 | | * |
9 | | * Developed at SunPro, a Sun Microsystems, Inc. business. |
10 | | * Permission to use, copy, modify, and distribute this |
11 | | * software is freely granted, provided that this notice |
12 | | * is preserved. |
13 | | * ==================================================== |
14 | | */ |
15 | | |
16 | | use super::{fabsf, j0f, j1f, logf, y0f, y1f}; |
17 | | |
18 | | /// Integer order of the [Bessel function](https://en.wikipedia.org/wiki/Bessel_function) of the first kind (f32). |
19 | 0 | pub fn jnf(n: i32, mut x: f32) -> f32 { |
20 | 0 | let mut ix: u32; |
21 | 0 | let mut nm1: i32; |
22 | 0 | let mut sign: bool; |
23 | 0 | let mut i: i32; |
24 | 0 | let mut a: f32; |
25 | 0 | let mut b: f32; |
26 | 0 | let mut temp: f32; |
27 | 0 |
|
28 | 0 | ix = x.to_bits(); |
29 | 0 | sign = (ix >> 31) != 0; |
30 | 0 | ix &= 0x7fffffff; |
31 | 0 | if ix > 0x7f800000 { |
32 | | /* nan */ |
33 | 0 | return x; |
34 | 0 | } |
35 | 0 |
|
36 | 0 | /* J(-n,x) = J(n,-x), use |n|-1 to avoid overflow in -n */ |
37 | 0 | if n == 0 { |
38 | 0 | return j0f(x); |
39 | 0 | } |
40 | 0 | if n < 0 { |
41 | 0 | nm1 = -(n + 1); |
42 | 0 | x = -x; |
43 | 0 | sign = !sign; |
44 | 0 | } else { |
45 | 0 | nm1 = n - 1; |
46 | 0 | } |
47 | 0 | if nm1 == 0 { |
48 | 0 | return j1f(x); |
49 | 0 | } |
50 | 0 |
|
51 | 0 | sign &= (n & 1) != 0; /* even n: 0, odd n: signbit(x) */ |
52 | 0 | x = fabsf(x); |
53 | 0 | if ix == 0 || ix == 0x7f800000 { |
54 | 0 | /* if x is 0 or inf */ |
55 | 0 | b = 0.0; |
56 | 0 | } else if (nm1 as f32) < x { |
57 | | /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ |
58 | 0 | a = j0f(x); |
59 | 0 | b = j1f(x); |
60 | 0 | i = 0; |
61 | 0 | while i < nm1 { |
62 | 0 | i += 1; |
63 | 0 | temp = b; |
64 | 0 | b = b * (2.0 * (i as f32) / x) - a; |
65 | 0 | a = temp; |
66 | 0 | } |
67 | | } else { |
68 | 0 | if ix < 0x35800000 { |
69 | | /* x < 2**-20 */ |
70 | | /* x is tiny, return the first Taylor expansion of J(n,x) |
71 | | * J(n,x) = 1/n!*(x/2)^n - ... |
72 | | */ |
73 | 0 | if nm1 > 8 { |
74 | 0 | /* underflow */ |
75 | 0 | nm1 = 8; |
76 | 0 | } |
77 | 0 | temp = 0.5 * x; |
78 | 0 | b = temp; |
79 | 0 | a = 1.0; |
80 | 0 | i = 2; |
81 | 0 | while i <= nm1 + 1 { |
82 | 0 | a *= i as f32; /* a = n! */ |
83 | 0 | b *= temp; /* b = (x/2)^n */ |
84 | 0 | i += 1; |
85 | 0 | } |
86 | 0 | b = b / a; |
87 | | } else { |
88 | | /* use backward recurrence */ |
89 | | /* x x^2 x^2 |
90 | | * J(n,x)/J(n-1,x) = ---- ------ ------ ..... |
91 | | * 2n - 2(n+1) - 2(n+2) |
92 | | * |
93 | | * 1 1 1 |
94 | | * (for large x) = ---- ------ ------ ..... |
95 | | * 2n 2(n+1) 2(n+2) |
96 | | * -- - ------ - ------ - |
97 | | * x x x |
98 | | * |
99 | | * Let w = 2n/x and h=2/x, then the above quotient |
100 | | * is equal to the continued fraction: |
101 | | * 1 |
102 | | * = ----------------------- |
103 | | * 1 |
104 | | * w - ----------------- |
105 | | * 1 |
106 | | * w+h - --------- |
107 | | * w+2h - ... |
108 | | * |
109 | | * To determine how many terms needed, let |
110 | | * Q(0) = w, Q(1) = w(w+h) - 1, |
111 | | * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), |
112 | | * When Q(k) > 1e4 good for single |
113 | | * When Q(k) > 1e9 good for double |
114 | | * When Q(k) > 1e17 good for quadruple |
115 | | */ |
116 | | /* determine k */ |
117 | | let mut t: f32; |
118 | | let mut q0: f32; |
119 | | let mut q1: f32; |
120 | | let mut w: f32; |
121 | | let h: f32; |
122 | | let mut z: f32; |
123 | | let mut tmp: f32; |
124 | | let nf: f32; |
125 | | let mut k: i32; |
126 | | |
127 | 0 | nf = (nm1 as f32) + 1.0; |
128 | 0 | w = 2.0 * (nf as f32) / x; |
129 | 0 | h = 2.0 / x; |
130 | 0 | z = w + h; |
131 | 0 | q0 = w; |
132 | 0 | q1 = w * z - 1.0; |
133 | 0 | k = 1; |
134 | 0 | while q1 < 1.0e4 { |
135 | 0 | k += 1; |
136 | 0 | z += h; |
137 | 0 | tmp = z * q1 - q0; |
138 | 0 | q0 = q1; |
139 | 0 | q1 = tmp; |
140 | 0 | } |
141 | 0 | t = 0.0; |
142 | 0 | i = k; |
143 | 0 | while i >= 0 { |
144 | 0 | t = 1.0 / (2.0 * ((i as f32) + nf) / x - t); |
145 | 0 | i -= 1; |
146 | 0 | } |
147 | 0 | a = t; |
148 | 0 | b = 1.0; |
149 | 0 | /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) |
150 | 0 | * Hence, if n*(log(2n/x)) > ... |
151 | 0 | * single 8.8722839355e+01 |
152 | 0 | * double 7.09782712893383973096e+02 |
153 | 0 | * long double 1.1356523406294143949491931077970765006170e+04 |
154 | 0 | * then recurrent value may overflow and the result is |
155 | 0 | * likely underflow to zero |
156 | 0 | */ |
157 | 0 | tmp = nf * logf(fabsf(w)); |
158 | 0 | if tmp < 88.721679688 { |
159 | 0 | i = nm1; |
160 | 0 | while i > 0 { |
161 | 0 | temp = b; |
162 | 0 | b = 2.0 * (i as f32) * b / x - a; |
163 | 0 | a = temp; |
164 | 0 | i -= 1; |
165 | 0 | } |
166 | | } else { |
167 | 0 | i = nm1; |
168 | 0 | while i > 0 { |
169 | 0 | temp = b; |
170 | 0 | b = 2.0 * (i as f32) * b / x - a; |
171 | 0 | a = temp; |
172 | 0 | /* scale b to avoid spurious overflow */ |
173 | 0 | let x1p60 = f32::from_bits(0x5d800000); // 0x1p60 == 2^60 |
174 | 0 | if b > x1p60 { |
175 | 0 | a /= b; |
176 | 0 | t /= b; |
177 | 0 | b = 1.0; |
178 | 0 | } |
179 | 0 | i -= 1; |
180 | | } |
181 | | } |
182 | 0 | z = j0f(x); |
183 | 0 | w = j1f(x); |
184 | 0 | if fabsf(z) >= fabsf(w) { |
185 | 0 | b = t * z / b; |
186 | 0 | } else { |
187 | 0 | b = t * w / a; |
188 | 0 | } |
189 | | } |
190 | | } |
191 | | |
192 | 0 | if sign { -b } else { b } |
193 | 0 | } |
194 | | |
195 | | /// Integer order of the [Bessel function](https://en.wikipedia.org/wiki/Bessel_function) of the second kind (f32). |
196 | 0 | pub fn ynf(n: i32, x: f32) -> f32 { |
197 | 0 | let mut ix: u32; |
198 | 0 | let mut ib: u32; |
199 | 0 | let nm1: i32; |
200 | 0 | let mut sign: bool; |
201 | 0 | let mut i: i32; |
202 | 0 | let mut a: f32; |
203 | 0 | let mut b: f32; |
204 | 0 | let mut temp: f32; |
205 | 0 |
|
206 | 0 | ix = x.to_bits(); |
207 | 0 | sign = (ix >> 31) != 0; |
208 | 0 | ix &= 0x7fffffff; |
209 | 0 | if ix > 0x7f800000 { |
210 | | /* nan */ |
211 | 0 | return x; |
212 | 0 | } |
213 | 0 | if sign && ix != 0 { |
214 | | /* x < 0 */ |
215 | 0 | return 0.0 / 0.0; |
216 | 0 | } |
217 | 0 | if ix == 0x7f800000 { |
218 | 0 | return 0.0; |
219 | 0 | } |
220 | 0 |
|
221 | 0 | if n == 0 { |
222 | 0 | return y0f(x); |
223 | 0 | } |
224 | 0 | if n < 0 { |
225 | 0 | nm1 = -(n + 1); |
226 | 0 | sign = (n & 1) != 0; |
227 | 0 | } else { |
228 | 0 | nm1 = n - 1; |
229 | 0 | sign = false; |
230 | 0 | } |
231 | 0 | if nm1 == 0 { |
232 | 0 | if sign { |
233 | 0 | return -y1f(x); |
234 | | } else { |
235 | 0 | return y1f(x); |
236 | | } |
237 | 0 | } |
238 | 0 |
|
239 | 0 | a = y0f(x); |
240 | 0 | b = y1f(x); |
241 | 0 | /* quit if b is -inf */ |
242 | 0 | ib = b.to_bits(); |
243 | 0 | i = 0; |
244 | 0 | while i < nm1 && ib != 0xff800000 { |
245 | 0 | i += 1; |
246 | 0 | temp = b; |
247 | 0 | b = (2.0 * (i as f32) / x) * b - a; |
248 | 0 | ib = b.to_bits(); |
249 | 0 | a = temp; |
250 | 0 | } |
251 | | |
252 | 0 | if sign { -b } else { b } |
253 | 0 | } |