1 /* origin: FreeBSD /usr/src/lib/msun/src/e_jnf.c */
3 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
13 * ====================================================
19 float jnf(int n, float x)
25 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
26 * Thus, J(-n,x) = J(n,-x)
28 GET_FLOAT_WORD(hx, x);
30 /* if J(n,NaN) is NaN */
38 if (n == 0) return j0f(x);
39 if (n == 1) return j1f(x);
41 sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
43 if (ix == 0 || ix >= 0x7f800000) /* if x is 0 or inf */
45 else if((float)n <= x) {
46 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
51 b = b*((float)(i+i)/x) - a; /* avoid underflow */
55 if (ix < 0x30800000) { /* x < 2**-29 */
56 /* x is tiny, return the first Taylor expansion of J(n,x)
57 * J(n,x) = 1/n!*(x/2)^n - ...
59 if (n > 33) /* underflow */
64 for (a=1.0f,i=2; i<=n; i++) {
65 a *= (float)i; /* a = n! */
66 b *= temp; /* b = (x/2)^n */
71 /* use backward recurrence */
73 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
74 * 2n - 2(n+1) - 2(n+2)
77 * (for large x) = ---- ------ ------ .....
79 * -- - ------ - ------ -
82 * Let w = 2n/x and h=2/x, then the above quotient
83 * is equal to the continued fraction:
85 * = -----------------------
87 * w - -----------------
92 * To determine how many terms needed, let
93 * Q(0) = w, Q(1) = w(w+h) - 1,
94 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
95 * When Q(k) > 1e4 good for single
96 * When Q(k) > 1e9 good for double
97 * When Q(k) > 1e17 good for quadruple
110 while (q1 < 1.0e9f) {
118 for (t=0.0f, i = 2*(n+k); i>=m; i -= 2)
122 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
123 * Hence, if n*(log(2n/x)) > ...
124 * single 8.8722839355e+01
125 * double 7.09782712893383973096e+02
126 * long double 1.1356523406294143949491931077970765006170e+04
127 * then recurrent value may overflow and the result is
128 * likely underflow to zero
132 tmp = tmp*logf(fabsf(v*tmp));
133 if (tmp < 88.721679688f) {
134 for (i=n-1,di=(float)(i+i); i>0; i--) {
142 for (i=n-1,di=(float)(i+i); i>0; i--){
148 /* scale b to avoid spurious overflow */
158 if (fabsf(z) >= fabsf(w))
164 if (sgn == 1) return -b;
168 float ynf(int n, float x)
174 GET_FLOAT_WORD(hx, x);
175 ix = 0x7fffffff & hx;
176 /* if Y(n,NaN) is NaN */
186 sign = 1 - ((n&1)<<1);
192 if (ix == 0x7f800000)
197 /* quit if b is -inf */
198 GET_FLOAT_WORD(ib,b);
199 for (i = 1; i < n && ib != 0xff800000; i++){
201 b = ((float)(i+i)/x)*b - a;
202 GET_FLOAT_WORD(ib, b);