1 /* e_jnf.c -- float version of e_jn.c.
2 * 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 * ====================================================
17 static char rcsid
[] = "$FreeBSD: src/lib/msun/src/e_jnf.c,v 1.8 2002/05/28 18:15:04 alfred Exp $";
21 #include "math_private.h"
24 invsqrtpi
= 5.6418961287e-01, /* 0x3f106ebb */
25 two
= 2.0000000000e+00, /* 0x40000000 */
26 one
= 1.0000000000e+00; /* 0x3F800000 */
28 static const float zero
= 0.0000000000e+00;
31 __ieee754_jnf(int n
, float x
)
37 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
38 * Thus, J(-n,x) = J(n,-x)
42 /* if J(n,NaN) is NaN */
43 if(ix
>0x7f800000) return x
+x
;
49 if(n
==0) return(__ieee754_j0f(x
));
50 if(n
==1) return(__ieee754_j1f(x
));
51 sgn
= (n
&1)&(hx
>>31); /* even n -- 0, odd n -- sign(x) */
53 if(ix
==0||ix
>=0x7f800000) /* if x is 0 or inf */
55 else if((float)n
<=x
) {
56 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
61 b
= b
*((float)(i
+i
)/x
) - a
; /* avoid underflow */
65 if(ix
<0x30800000) { /* x < 2**-29 */
66 /* x is tiny, return the first Taylor expansion of J(n,x)
67 * J(n,x) = 1/n!*(x/2)^n - ...
69 if(n
>33) /* underflow */
72 temp
= x
*(float)0.5; b
= temp
;
73 for (a
=one
,i
=2;i
<=n
;i
++) {
74 a
*= (float)i
; /* a = n! */
75 b
*= temp
; /* b = (x/2)^n */
80 /* use backward recurrence */
82 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
83 * 2n - 2(n+1) - 2(n+2)
86 * (for large x) = ---- ------ ------ .....
88 * -- - ------ - ------ -
91 * Let w = 2n/x and h=2/x, then the above quotient
92 * is equal to the continued fraction:
94 * = -----------------------
96 * w - -----------------
101 * To determine how many terms needed, let
102 * Q(0) = w, Q(1) = w(w+h) - 1,
103 * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
104 * When Q(k) > 1e4 good for single
105 * When Q(k) > 1e9 good for double
106 * When Q(k) > 1e17 good for quadruple
110 float q0
,q1
,h
,tmp
; int32_t k
,m
;
111 w
= (n
+n
)/(float)x
; h
= (float)2.0/(float)x
;
112 q0
= w
; z
= w
+h
; q1
= w
*z
- (float)1.0; k
=1;
113 while(q1
<(float)1.0e9
) {
120 for(t
=zero
, i
= 2*(n
+k
); i
>=m
; i
-= 2) t
= one
/(i
/x
-t
);
123 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
124 * Hence, if n*(log(2n/x)) > ...
125 * single 8.8722839355e+01
126 * double 7.09782712893383973096e+02
127 * long double 1.1356523406294143949491931077970765006170e+04
128 * then recurrent value may overflow and the result is
129 * likely underflow to zero
133 tmp
= tmp
*__ieee754_logf(fabsf(v
*tmp
));
134 if(tmp
<(float)8.8721679688e+01) {
135 for(i
=n
-1,di
=(float)(i
+i
);i
>0;i
--){
143 for(i
=n
-1,di
=(float)(i
+i
);i
>0;i
--){
149 /* scale b to avoid spurious overflow */
157 b
= (t
*__ieee754_j0f(x
)/b
);
160 if(sgn
==1) return -b
; else return b
;
164 __ieee754_ynf(int n
, float x
)
170 GET_FLOAT_WORD(hx
,x
);
172 /* if Y(n,NaN) is NaN */
173 if(ix
>0x7f800000) return x
+x
;
174 if(ix
==0) return -one
/zero
;
175 if(hx
<0) return zero
/zero
;
179 sign
= 1 - ((n
&1)<<1);
181 if(n
==0) return(__ieee754_y0f(x
));
182 if(n
==1) return(sign
*__ieee754_y1f(x
));
183 if(ix
==0x7f800000) return zero
;
185 a
= __ieee754_y0f(x
);
186 b
= __ieee754_y1f(x
);
187 /* quit if b is -inf */
188 GET_FLOAT_WORD(ib
,b
);
189 for(i
=1;i
<n
&&ib
!=0xff800000;i
++){
191 b
= ((float)(i
+i
)/x
)*b
- a
;
192 GET_FLOAT_WORD(ib
,b
);
195 if(sign
>0) return b
; else return -b
;