Print this page
5261 libm should stop using synonyms.h
5298 fabs is 0-sized, confuses dis(1) and others
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Gordon Ross <gwr@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/jn.c
+++ new/usr/src/lib/libm/common/C/jn.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 -#pragma weak jn = __jn
31 -#pragma weak yn = __yn
30 +#pragma weak __jn = jn
31 +#pragma weak __yn = yn
32 32
33 33 /*
34 34 * floating point Bessel's function of the 1st and 2nd kind
35 35 * of order n: jn(n,x),yn(n,x);
36 36 *
37 37 * Special cases:
38 38 * y0(0)=y1(0)=yn(n,0) = -inf with division by zero signal;
39 39 * y0(-ve)=y1(-ve)=yn(n,-ve) are NaN with invalid signal.
40 40 * Note 2. About jn(n,x), yn(n,x)
41 41 * For n=0, j0(x) is called,
42 42 * for n=1, j1(x) is called,
43 43 * for n<x, forward recursion us used starting
44 44 * from values of j0(x) and j1(x).
45 45 * for n>x, a continued fraction approximation to
46 46 * j(n,x)/j(n-1,x) is evaluated and then backward
47 47 * recursion is used starting from a supposed value
48 48 * for j(n,x). The resulting value of j(0,x) is
49 49 * compared with the actual value to correct the
50 50 * supposed value of j(n,x).
51 51 *
52 52 * yn(n,x) is similar in all respects, except
53 53 * that forward recursion is used for all
54 54 * values of n>1.
55 55 *
56 56 */
57 57
58 58 #include "libm.h"
59 59 #include <float.h> /* DBL_MIN */
60 60 #include <values.h> /* X_TLOSS */
61 61 #include "xpg6.h" /* __xpg6 */
62 62
63 63 #define GENERIC double
64 64
65 65 static const GENERIC
66 66 invsqrtpi = 5.641895835477562869480794515607725858441e-0001,
67 67 two = 2.0,
68 68 zero = 0.0,
69 69 one = 1.0;
70 70
71 71 GENERIC
72 72 jn(int n, GENERIC x) {
73 73 int i, sgn;
74 74 GENERIC a, b, temp = 0;
75 75 GENERIC z, w, ox, on;
76 76
77 77 /*
78 78 * J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
79 79 * Thus, J(-n,x) = J(n,-x)
80 80 */
81 81 ox = x; on = (GENERIC)n;
82 82 if (n < 0) {
83 83 n = -n;
84 84 x = -x;
85 85 }
86 86 if (isnan(x))
87 87 return (x*x); /* + -> * for Cheetah */
88 88 if (!((int) _lib_version == libm_ieee ||
89 89 (__xpg6 & _C99SUSv3_math_errexcept) != 0)) {
90 90 if (fabs(x) > X_TLOSS)
91 91 return (_SVID_libm_err(on, ox, 38));
92 92 }
93 93 if (n == 0)
94 94 return (j0(x));
95 95 if (n == 1)
96 96 return (j1(x));
97 97 if ((n&1) == 0)
98 98 sgn = 0; /* even n */
99 99 else
100 100 sgn = signbit(x); /* old n */
101 101 x = fabs(x);
102 102 if (x == zero||!finite(x)) b = zero;
103 103 else if ((GENERIC)n <= x) {
104 104 /*
105 105 * Safe to use
106 106 * J(n+1,x)=2n/x *J(n,x)-J(n-1,x)
107 107 */
108 108 if (x > 1.0e91) {
109 109 /*
110 110 * x >> n**2
111 111 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
112 112 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
113 113 * Let s=sin(x), c=cos(x),
114 114 * xn=x-(2n+1)*pi/4, sqt2 = sqrt(2),then
115 115 *
116 116 * n sin(xn)*sqt2 cos(xn)*sqt2
117 117 * ----------------------------------
118 118 * 0 s-c c+s
119 119 * 1 -s-c -c+s
120 120 * 2 -s+c -c-s
121 121 * 3 s+c c-s
122 122 */
123 123 switch (n&3) {
124 124 case 0: temp = cos(x)+sin(x); break;
125 125 case 1: temp = -cos(x)+sin(x); break;
126 126 case 2: temp = -cos(x)-sin(x); break;
127 127 case 3: temp = cos(x)-sin(x); break;
128 128 }
129 129 b = invsqrtpi*temp/sqrt(x);
130 130 } else {
131 131 a = j0(x);
132 132 b = j1(x);
133 133 for (i = 1; i < n; i++) {
134 134 temp = b;
135 135 b = b*((GENERIC)(i+i)/x) - a; /* avoid underflow */
136 136 a = temp;
137 137 }
138 138 }
139 139 } else {
140 140 if (x < 1e-9) { /* use J(n,x) = 1/n!*(x/2)^n */
141 141 b = pow(0.5*x, (GENERIC) n);
142 142 if (b != zero) {
143 143 for (a = one, i = 1; i <= n; i++) a *= (GENERIC)i;
144 144 b = b/a;
145 145 }
146 146 } else {
147 147 /*
148 148 * use backward recurrence
149 149 * x x^2 x^2
150 150 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
151 151 * 2n - 2(n+1) - 2(n+2)
152 152 *
153 153 * 1 1 1
154 154 * (for large x) = ---- ------ ------ .....
155 155 * 2n 2(n+1) 2(n+2)
156 156 * -- - ------ - ------ -
157 157 * x x x
158 158 *
159 159 * Let w = 2n/x and h = 2/x, then the above quotient
160 160 * is equal to the continued fraction:
161 161 * 1
162 162 * = -----------------------
163 163 * 1
164 164 * w - -----------------
165 165 * 1
166 166 * w+h - ---------
167 167 * w+2h - ...
168 168 *
169 169 * To determine how many terms needed, let
170 170 * Q(0) = w, Q(1) = w(w+h) - 1,
171 171 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
172 172 * When Q(k) > 1e4 good for single
173 173 * When Q(k) > 1e9 good for double
174 174 * When Q(k) > 1e17 good for quaduple
175 175 */
176 176 /* determin k */
177 177 GENERIC t, v;
178 178 double q0, q1, h, tmp; int k, m;
179 179 w = (n+n)/(double)x; h = 2.0/(double)x;
180 180 q0 = w; z = w + h; q1 = w*z - 1.0; k = 1;
181 181 while (q1 < 1.0e9) {
182 182 k += 1; z += h;
183 183 tmp = z*q1 - q0;
184 184 q0 = q1;
185 185 q1 = tmp;
186 186 }
187 187 m = n+n;
188 188 for (t = zero, i = 2*(n+k); i >= m; i -= 2) t = one/(i/x-t);
189 189 a = t;
190 190 b = one;
191 191 /*
192 192 * estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
193 193 * hence, if n*(log(2n/x)) > ...
194 194 * single 8.8722839355e+01
195 195 * double 7.09782712893383973096e+02
196 196 * long double 1.1356523406294143949491931077970765006170e+04
197 197 * then recurrent value may overflow and the result is
198 198 * likely underflow to zero
199 199 */
200 200 tmp = n;
201 201 v = two/x;
202 202 tmp = tmp*log(fabs(v*tmp));
203 203 if (tmp < 7.09782712893383973096e+02) {
204 204 for (i = n-1; i > 0; i--) {
205 205 temp = b;
206 206 b = ((i+i)/x)*b - a;
207 207 a = temp;
208 208 }
209 209 } else {
210 210 for (i = n-1; i > 0; i--) {
211 211 temp = b;
212 212 b = ((i+i)/x)*b - a;
213 213 a = temp;
214 214 if (b > 1e100) {
215 215 a /= b;
216 216 t /= b;
217 217 b = 1.0;
218 218 }
219 219 }
220 220 }
221 221 b = (t*j0(x)/b);
222 222 }
223 223 }
224 224 if (sgn == 1)
225 225 return (-b);
226 226 else
227 227 return (b);
228 228 }
229 229
230 230 GENERIC
231 231 yn(int n, GENERIC x) {
232 232 int i;
233 233 int sign;
234 234 GENERIC a, b, temp = 0, ox, on;
235 235
236 236 ox = x; on = (GENERIC)n;
237 237 if (isnan(x))
238 238 return (x*x); /* + -> * for Cheetah */
239 239 if (x <= zero) {
240 240 if (x == zero) {
241 241 /* return -one/zero; */
242 242 return (_SVID_libm_err((GENERIC)n, x, 12));
243 243 } else {
244 244 /* return zero/zero; */
245 245 return (_SVID_libm_err((GENERIC)n, x, 13));
246 246 }
247 247 }
248 248 if (!((int) _lib_version == libm_ieee ||
249 249 (__xpg6 & _C99SUSv3_math_errexcept) != 0)) {
250 250 if (x > X_TLOSS)
251 251 return (_SVID_libm_err(on, ox, 39));
252 252 }
253 253 sign = 1;
254 254 if (n < 0) {
255 255 n = -n;
256 256 if ((n&1) == 1) sign = -1;
257 257 }
258 258 if (n == 0)
259 259 return (y0(x));
260 260 if (n == 1)
261 261 return (sign*y1(x));
262 262 if (!finite(x))
263 263 return (zero);
264 264
265 265 if (x > 1.0e91) {
266 266 /*
267 267 * x >> n**2
268 268 * Jn(x) = cos(x-(2n+1)*pi/4)*sqrt(2/x*pi)
269 269 * Yn(x) = sin(x-(2n+1)*pi/4)*sqrt(2/x*pi)
270 270 * Let s = sin(x), c = cos(x),
271 271 * xn = x-(2n+1)*pi/4, sqt2 = sqrt(2), then
272 272 *
273 273 * n sin(xn)*sqt2 cos(xn)*sqt2
274 274 * ----------------------------------
275 275 * 0 s-c c+s
276 276 * 1 -s-c -c+s
277 277 * 2 -s+c -c-s
278 278 * 3 s+c c-s
279 279 */
280 280 switch (n&3) {
281 281 case 0: temp = sin(x)-cos(x); break;
282 282 case 1: temp = -sin(x)-cos(x); break;
283 283 case 2: temp = -sin(x)+cos(x); break;
284 284 case 3: temp = sin(x)+cos(x); break;
285 285 }
286 286 b = invsqrtpi*temp/sqrt(x);
287 287 } else {
288 288 a = y0(x);
289 289 b = y1(x);
290 290 /*
291 291 * fix 1262058 and take care of non-default rounding
292 292 */
293 293 for (i = 1; i < n; i++) {
294 294 temp = b;
295 295 b *= (GENERIC) (i + i) / x;
296 296 if (b <= -DBL_MAX)
297 297 break;
298 298 b -= a;
299 299 a = temp;
300 300 }
301 301 }
302 302 if (sign > 0)
303 303 return (b);
304 304 else
305 305 return (-b);
306 306 }
↓ open down ↓ |
265 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX