1 /* ef_jn.c -- float version of e_jn.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 */
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 #include "fdlibm.h"
17
18 static const float two = 2.0000000000e+00, /* 0x40000000 */
19 one = 1.0000000000e+00; /* 0x3F800000 */
20
21 static const float zero = 0.0000000000e+00;
22
23 float
jnf(int n,float x)24 jnf(int n, float x)
25 {
26 __int32_t i, hx, ix, sgn;
27 float a, b, temp, di;
28 float z, w;
29
30 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
31 * Thus, J(-n,x) = J(n,-x)
32 */
33 GET_FLOAT_WORD(hx, x);
34 ix = 0x7fffffff & hx;
35 /* if J(n,NaN) is NaN */
36 if (FLT_UWORD_IS_NAN(ix))
37 return x + x;
38 if (n < 0) {
39 n = -n;
40 x = -x;
41 hx ^= 0x80000000;
42 }
43 if (n == 0)
44 return (j0f(x));
45 if (n == 1)
46 return (j1f(x));
47 sgn = (n & 1) & (hx >> 31); /* even n -- 0, odd n -- sign(x) */
48 x = fabsf(x);
49 if (FLT_UWORD_IS_ZERO(ix) || FLT_UWORD_IS_INFINITE(ix))
50 b = zero;
51 else if ((float)n <= x) {
52 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
53 a = j0f(x);
54 b = j1f(x);
55 for (i = 1; i < n; i++) {
56 temp = b;
57 b = b * ((float)(i + i) / x) - a; /* avoid underflow */
58 a = temp;
59 }
60 } else {
61 if (ix < 0x30800000) { /* x < 2**-29 */
62 /* x is tiny, return the first Taylor expansion of J(n,x)
63 * J(n,x) = 1/n!*(x/2)^n - ...
64 */
65 if (n > 33) /* underflow */
66 b = zero;
67 else {
68 temp = x * (float)0.5;
69 b = temp;
70 for (a = one, i = 2; i <= n; i++) {
71 a *= (float)i; /* a = n! */
72 b *= temp; /* b = (x/2)^n */
73 }
74 b = b / a;
75 }
76 } else {
77 /* use backward recurrence */
78 /* x x^2 x^2
79 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
80 * 2n - 2(n+1) - 2(n+2)
81 *
82 * 1 1 1
83 * (for large x) = ---- ------ ------ .....
84 * 2n 2(n+1) 2(n+2)
85 * -- - ------ - ------ -
86 * x x x
87 *
88 * Let w = 2n/x and h=2/x, then the above quotient
89 * is equal to the continued fraction:
90 * 1
91 * = -----------------------
92 * 1
93 * w - -----------------
94 * 1
95 * w+h - ---------
96 * w+2h - ...
97 *
98 * To determine how many terms needed, let
99 * Q(0) = w, Q(1) = w(w+h) - 1,
100 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
101 * When Q(k) > 1e4 good for single
102 * When Q(k) > 1e9 good for double
103 * When Q(k) > 1e17 good for quadruple
104 */
105 /* determine k */
106 float t, v;
107 float q0, q1, h, tmp;
108 __int32_t k, m;
109 w = (n + n) / (float)x;
110 h = (float)2.0 / (float)x;
111 q0 = w;
112 z = w + h;
113 q1 = w * z - (float)1.0;
114 k = 1;
115 while (q1 < (float)1.0e9) {
116 k += 1;
117 z += h;
118 tmp = z * q1 - q0;
119 q0 = q1;
120 q1 = tmp;
121 }
122 m = n + n;
123 for (t = zero, i = 2 * (n + k); i >= m; i -= 2)
124 t = one / (i / x - t);
125 a = t;
126 b = one;
127 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
128 * Hence, if n*(log(2n/x)) > ...
129 * single 8.8722839355e+01
130 * double 7.09782712893383973096e+02
131 * long double 1.1356523406294143949491931077970765006170e+04
132 * then recurrent value may overflow and the result is
133 * likely underflow to zero
134 */
135 tmp = n;
136 v = two / x;
137 tmp = tmp * logf(fabsf(v * tmp));
138 if (tmp < (float)8.8721679688e+01) {
139 for (i = n - 1, di = (float)(i + i); i > 0; i--) {
140 temp = b;
141 b *= di;
142 b = b / x - a;
143 a = temp;
144 di -= two;
145 }
146 } else {
147 for (i = n - 1, di = (float)(i + i); i > 0; i--) {
148 temp = b;
149 b *= di;
150 b = b / x - a;
151 a = temp;
152 di -= two;
153 /* scale b to avoid spurious overflow */
154 if (b > (float)1e10) {
155 a /= b;
156 t /= b;
157 b = one;
158 }
159 }
160 }
161 b = (t * j0f(x) / b);
162 }
163 }
164 if (sgn == 1)
165 return -b;
166 else
167 return b;
168 }
169
170 float
ynf(int n,float x)171 ynf(int n, float x)
172 {
173 __int32_t i, hx, ix, ib;
174 __int32_t sign;
175 float a, b, temp;
176
177 GET_FLOAT_WORD(hx, x);
178 ix = 0x7fffffff & hx;
179
180 if (ix == 0)
181 return __math_divzerof(1);
182
183 if (ix > 0x7f800000)
184 return x+x;
185
186 if (hx < 0)
187 return __math_invalidf(x);
188
189 if (ix == 0x7f800000)
190 return zero;
191
192 sign = 1;
193 if (n < 0) {
194 n = -n;
195 sign = 1 - ((n & 1) << 1);
196 }
197 if (n == 0)
198 return (y0f(x));
199 if (n == 1)
200 return (sign * y1f(x));
201
202 a = y0f(x);
203 b = y1f(x);
204 /* quit if b is -inf */
205 GET_FLOAT_WORD(ib, b);
206 for (i = 1; i < n && ib != (__int32_t)0xff800000; i++) {
207 temp = b;
208 b = ((float)(i + i) / x) * b - a;
209 GET_FLOAT_WORD(ib, b);
210 a = temp;
211 }
212 if (sign > 0)
213 return b;
214 else
215 return -b;
216 }
217
218 _MATH_ALIAS_f_if(jn)
219
220 _MATH_ALIAS_f_if(yn)
221