Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* e_jnf.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 | #ifndef lint |
||
17 | static char rcsid[] = "$\Id: e_jnf.c,v 1.3 1995/05/30 05:48:25 rgrimes Exp $"; |
||
18 | #endif |
||
19 | |||
20 | #include "math.h" |
||
21 | #include "math_private.h" |
||
22 | |||
23 | #ifdef __STDC__ |
||
24 | static const float |
||
25 | #else |
||
26 | static float |
||
27 | #endif |
||
28 | invsqrtpi= 5.6418961287e-01, /* 0x3f106ebb */ |
||
29 | two = 2.0000000000e+00, /* 0x40000000 */ |
||
30 | one = 1.0000000000e+00; /* 0x3F800000 */ |
||
31 | |||
32 | #ifdef __STDC__ |
||
33 | static const float zero = 0.0000000000e+00; |
||
34 | #else |
||
35 | static float zero = 0.0000000000e+00; |
||
36 | #endif |
||
37 | |||
38 | #ifdef __STDC__ |
||
39 | float __ieee754_jnf(int n, float x) |
||
40 | #else |
||
41 | float __ieee754_jnf(n,x) |
||
42 | int n; float x; |
||
43 | #endif |
||
44 | { |
||
45 | int32_t i,hx,ix, sgn; |
||
46 | float a, b, temp, di; |
||
47 | float z, w; |
||
48 | |||
49 | /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x) |
||
50 | * Thus, J(-n,x) = J(n,-x) |
||
51 | */ |
||
52 | GET_FLOAT_WORD(hx,x); |
||
53 | ix = 0x7fffffff&hx; |
||
54 | /* if J(n,NaN) is NaN */ |
||
55 | if(ix>0x7f800000) return x+x; |
||
56 | if(n<0){ |
||
57 | n = -n; |
||
58 | x = -x; |
||
59 | hx ^= 0x80000000; |
||
60 | } |
||
61 | if(n==0) return(__ieee754_j0f(x)); |
||
62 | if(n==1) return(__ieee754_j1f(x)); |
||
63 | sgn = (n&1)&(hx>>31); /* even n -- 0, odd n -- sign(x) */ |
||
64 | x = fabsf(x); |
||
65 | if(ix==0||ix>=0x7f800000) /* if x is 0 or inf */ |
||
66 | b = zero; |
||
67 | else if((float)n<=x) { |
||
68 | /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */ |
||
69 | a = __ieee754_j0f(x); |
||
70 | b = __ieee754_j1f(x); |
||
71 | for(i=1;i<n;i++){ |
||
72 | temp = b; |
||
73 | b = b*((float)(i+i)/x) - a; /* avoid underflow */ |
||
74 | a = temp; |
||
75 | } |
||
76 | } else { |
||
77 | if(ix<0x30800000) { /* x < 2**-29 */ |
||
78 | /* x is tiny, return the first Taylor expansion of J(n,x) |
||
79 | * J(n,x) = 1/n!*(x/2)^n - ... |
||
80 | */ |
||
81 | if(n>33) /* underflow */ |
||
82 | b = zero; |
||
83 | else { |
||
84 | temp = x*(float)0.5; b = temp; |
||
85 | for (a=one,i=2;i<=n;i++) { |
||
86 | a *= (float)i; /* a = n! */ |
||
87 | b *= temp; /* b = (x/2)^n */ |
||
88 | } |
||
89 | b = b/a; |
||
90 | } |
||
91 | } else { |
||
92 | /* use backward recurrence */ |
||
93 | /* x x^2 x^2 |
||
94 | * J(n,x)/J(n-1,x) = ---- ------ ------ ..... |
||
95 | * 2n - 2(n+1) - 2(n+2) |
||
96 | * |
||
97 | * 1 1 1 |
||
98 | * (for large x) = ---- ------ ------ ..... |
||
99 | * 2n 2(n+1) 2(n+2) |
||
100 | * -- - ------ - ------ - |
||
101 | * x x x |
||
102 | * |
||
103 | * Let w = 2n/x and h=2/x, then the above quotient |
||
104 | * is equal to the continued fraction: |
||
105 | * 1 |
||
106 | * = ----------------------- |
||
107 | * 1 |
||
108 | * w - ----------------- |
||
109 | * 1 |
||
110 | * w+h - --------- |
||
111 | * w+2h - ... |
||
112 | * |
||
113 | * To determine how many terms needed, let |
||
114 | * Q(0) = w, Q(1) = w(w+h) - 1, |
||
115 | * Q(k) = (w+k*h)*Q(k-1) - Q(k-2), |
||
116 | * When Q(k) > 1e4 good for single |
||
117 | * When Q(k) > 1e9 good for double |
||
118 | * When Q(k) > 1e17 good for quadruple |
||
119 | */ |
||
120 | /* determine k */ |
||
121 | float t,v; |
||
122 | float q0,q1,h,tmp; int32_t k,m; |
||
123 | w = (n+n)/(float)x; h = (float)2.0/(float)x; |
||
124 | q0 = w; z = w+h; q1 = w*z - (float)1.0; k=1; |
||
125 | while(q1<(float)1.0e9) { |
||
126 | k += 1; z += h; |
||
127 | tmp = z*q1 - q0; |
||
128 | q0 = q1; |
||
129 | q1 = tmp; |
||
130 | } |
||
131 | m = n+n; |
||
132 | for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t); |
||
133 | a = t; |
||
134 | b = one; |
||
135 | /* estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n) |
||
136 | * Hence, if n*(log(2n/x)) > ... |
||
137 | * single 8.8722839355e+01 |
||
138 | * double 7.09782712893383973096e+02 |
||
139 | * long double 1.1356523406294143949491931077970765006170e+04 |
||
140 | * then recurrent value may overflow and the result is |
||
141 | * likely underflow to zero |
||
142 | */ |
||
143 | tmp = n; |
||
144 | v = two/x; |
||
145 | tmp = tmp*__ieee754_logf(fabsf(v*tmp)); |
||
146 | if(tmp<(float)8.8721679688e+01) { |
||
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 | } |
||
154 | } else { |
||
155 | for(i=n-1,di=(float)(i+i);i>0;i--){ |
||
156 | temp = b; |
||
157 | b *= di; |
||
158 | b = b/x - a; |
||
159 | a = temp; |
||
160 | di -= two; |
||
161 | /* scale b to avoid spurious overflow */ |
||
162 | if(b>(float)1e10) { |
||
163 | a /= b; |
||
164 | t /= b; |
||
165 | b = one; |
||
166 | } |
||
167 | } |
||
168 | } |
||
169 | b = (t*__ieee754_j0f(x)/b); |
||
170 | } |
||
171 | } |
||
172 | if(sgn==1) return -b; else return b; |
||
173 | } |
||
174 | |||
175 | #ifdef __STDC__ |
||
176 | float __ieee754_ynf(int n, float x) |
||
177 | #else |
||
178 | float __ieee754_ynf(n,x) |
||
179 | int n; float x; |
||
180 | #endif |
||
181 | { |
||
182 | int32_t i,hx,ix,ib; |
||
183 | int32_t sign; |
||
184 | float a, b, temp; |
||
185 | |||
186 | GET_FLOAT_WORD(hx,x); |
||
187 | ix = 0x7fffffff&hx; |
||
188 | /* if Y(n,NaN) is NaN */ |
||
189 | if(ix>0x7f800000) return x+x; |
||
190 | if(ix==0) return -one/zero; |
||
191 | if(hx<0) return zero/zero; |
||
192 | sign = 1; |
||
193 | if(n<0){ |
||
194 | n = -n; |
||
195 | sign = 1 - ((n&1)<<1); |
||
196 | } |
||
197 | if(n==0) return(__ieee754_y0f(x)); |
||
198 | if(n==1) return(sign*__ieee754_y1f(x)); |
||
199 | if(ix==0x7f800000) return zero; |
||
200 | |||
201 | a = __ieee754_y0f(x); |
||
202 | b = __ieee754_y1f(x); |
||
203 | /* quit if b is -inf */ |
||
204 | GET_FLOAT_WORD(ib,b); |
||
205 | for(i=1;i<n&&ib!=0xff800000;i++){ |
||
206 | temp = b; |
||
207 | b = ((float)(i+i)/x)*b - a; |
||
208 | GET_FLOAT_WORD(ib,b); |
||
209 | a = temp; |
||
210 | } |
||
211 | if(sign>0) return b; else return -b; |
||
212 | } |