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 * ====================================================
15 * $FreeBSD: head/lib/msun/src/e_jnf.c 215237 2010-11-13 10:54:10Z uqs $
19 #include "math_private.h"
22 two = 2.0000000000e+00, /* 0x40000000 */
23 one = 1.0000000000e+00; /* 0x3F800000 */
25 static const float zero = 0.0000000000e+00;
28 __ieee754_jnf(int n, float x)
34 /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
35 * Thus, J(-n,x) = J(n,-x)
39 /* if J(n,NaN) is NaN */
40 if(ix>0x7f800000) return x+x;
46 if(n==0) return(__ieee754_j0f(x));
47 if(n==1) return(__ieee754_j1f(x));
48 sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */
50 if(ix==0||ix>=0x7f800000) /* if x is 0 or inf */
52 else if((float)n<=x) {
53 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
58 b = b*((float)(i+i)/x) - a; /* avoid underflow */
62 if(ix<0x30800000) { /* x < 2**-29 */
63 /* x is tiny, return the first Taylor expansion of J(n,x)
64 * J(n,x) = 1/n!*(x/2)^n - ...
66 if(n>33) /* underflow */
69 temp = x*(float)0.5; b = temp;
70 for (a=one,i=2;i<=n;i++) {
71 a *= (float)i; /* a = n! */
72 b *= temp; /* b = (x/2)^n */
77 /* use backward recurrence */
79 * J(n,x)/J(n-1,x) = ---- ------ ------ .....
80 * 2n - 2(n+1) - 2(n+2)
83 * (for large x) = ---- ------ ------ .....
85 * -- - ------ - ------ -
88 * Let w = 2n/x and h=2/x, then the above quotient
89 * is equal to the continued fraction:
91 * = -----------------------
93 * w - -----------------
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
107 float q0,q1,h,tmp; int32_t k,m;
108 w = (n+n)/(float)x; h = (float)2.0/(float)x;
109 q0 = w; z = w+h; q1 = w*z - (float)1.0; k=1;
110 while(q1<(float)1.0e9) {
117 for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
120 /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
121 * Hence, if n*(log(2n/x)) > ...
122 * single 8.8722839355e+01
123 * double 7.09782712893383973096e+02
124 * long double 1.1356523406294143949491931077970765006170e+04
125 * then recurrent value may overflow and the result is
126 * likely underflow to zero
130 tmp = tmp*__ieee754_logf(fabsf(v*tmp));
131 if(tmp<(float)8.8721679688e+01) {
132 for(i=n-1,di=(float)(i+i);i>0;i--){
140 for(i=n-1,di=(float)(i+i);i>0;i--){
146 /* scale b to avoid spurious overflow */
154 z = __ieee754_j0f(x);
155 w = __ieee754_j1f(x);
156 if (fabsf(z) >= fabsf(w))
162 if(sgn==1) return -b; else return b;
166 __ieee754_ynf(int n, float x)
172 GET_FLOAT_WORD(hx,x);
174 /* if Y(n,NaN) is NaN */
175 if(ix>0x7f800000) return x+x;
176 if(ix==0) return -one/zero;
177 if(hx<0) return zero/zero;
181 sign = 1 - ((n&1)<<1);
183 if(n==0) return(__ieee754_y0f(x));
184 if(n==1) return(sign*__ieee754_y1f(x));
185 if(ix==0x7f800000) return zero;
187 a = __ieee754_y0f(x);
188 b = __ieee754_y1f(x);
189 /* quit if b is -inf */
190 GET_FLOAT_WORD(ib,b);
191 for(i=1;i<n&&ib!=0xff800000;i++){
193 b = ((float)(i+i)/x)*b - a;
194 GET_FLOAT_WORD(ib,b);
197 if(sign>0) return b; else return -b;