math cleanup: use 1.0f instead of (float)1.0
[musl] / src / math / jnf.c
1 /* origin: FreeBSD /usr/src/lib/msun/src/e_jnf.c */
2 /*
3  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
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 "libm.h"
17
18 static const float
19 two = 2.0000000000e+00, /* 0x40000000 */
20 one = 1.0000000000e+00; /* 0x3F800000 */
21
22 static const float zero = 0.0000000000e+00;
23
24 float 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 (ix > 0x7f800000)
37                 return x+x;
38         if (n < 0) {
39                 n = -n;
40                 x = -x;
41                 hx ^= 0x80000000;
42         }
43         if (n == 0) return j0f(x);
44         if (n == 1) return j1f(x);
45
46         sgn = (n&1)&(hx>>31);  /* even n -- 0, odd n -- sign(x) */
47         x = fabsf(x);
48         if (ix == 0 || ix >= 0x7f800000)  /* if x is 0 or inf */
49                 b = zero;
50         else if((float)n <= x) {
51                 /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
52                 a = j0f(x);
53                 b = j1f(x);
54                 for (i=1; i<n; i++){
55                         temp = b;
56                         b = b*((float)(i+i)/x) - a; /* avoid underflow */
57                         a = temp;
58                 }
59         } else {
60                 if (ix < 0x30800000) { /* x < 2**-29 */
61                         /* x is tiny, return the first Taylor expansion of J(n,x)
62                          * J(n,x) = 1/n!*(x/2)^n  - ...
63                          */
64                         if (n > 33)  /* underflow */
65                                 b = zero;
66                         else {
67                                 temp = 0.5f * x;
68                                 b = temp;
69                                 for (a=one,i=2; i<=n; i++) {
70                                         a *= (float)i;    /* a = n! */
71                                         b *= temp;        /* b = (x/2)^n */
72                                 }
73                                 b = b/a;
74                         }
75                 } else {
76                         /* use backward recurrence */
77                         /*                      x      x^2      x^2
78                          *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
79                          *                      2n  - 2(n+1) - 2(n+2)
80                          *
81                          *                      1      1        1
82                          *  (for large x)   =  ----  ------   ------   .....
83                          *                      2n   2(n+1)   2(n+2)
84                          *                      -- - ------ - ------ -
85                          *                       x     x         x
86                          *
87                          * Let w = 2n/x and h=2/x, then the above quotient
88                          * is equal to the continued fraction:
89                          *                  1
90                          *      = -----------------------
91                          *                     1
92                          *         w - -----------------
93                          *                        1
94                          *              w+h - ---------
95                          *                     w+2h - ...
96                          *
97                          * To determine how many terms needed, let
98                          * Q(0) = w, Q(1) = w(w+h) - 1,
99                          * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
100                          * When Q(k) > 1e4      good for single
101                          * When Q(k) > 1e9      good for double
102                          * When Q(k) > 1e17     good for quadruple
103                          */
104                         /* determine k */
105                         float t,v;
106                         float q0,q1,h,tmp;
107                         int32_t k,m;
108
109                         w = (n+n)/x;
110                         h = 2.0f/x;
111                         z = w+h;
112                         q0 = w;
113                         q1 = w*z - 1.0f;
114                         k = 1;
115                         while (q1 < 1.0e9f) {
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 < 88.721679688f) {
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 > 1e10f) {
155                                                 a /= b;
156                                                 t /= b;
157                                                 b = one;
158                                         }
159                                 }
160                         }
161                         z = j0f(x);
162                         w = j1f(x);
163                         if (fabsf(z) >= fabsf(w))
164                                 b = t*z/b;
165                         else
166                                 b = t*w/a;
167                 }
168         }
169         if (sgn == 1) return -b;
170         return b;
171 }
172
173 float ynf(int n, float x)
174 {
175         int32_t i,hx,ix,ib;
176         int32_t sign;
177         float a, b, temp;
178
179         GET_FLOAT_WORD(hx, x);
180         ix = 0x7fffffff & hx;
181         /* if Y(n,NaN) is NaN */
182         if (ix > 0x7f800000)
183                 return x+x;
184         if (ix == 0)
185                 return -one/zero;
186         if (hx < 0)
187                 return zero/zero;
188         sign = 1;
189         if (n < 0) {
190                 n = -n;
191                 sign = 1 - ((n&1)<<1);
192         }
193         if (n == 0)
194                 return y0f(x);
195         if (n == 1)
196                 return sign*y1f(x);
197         if (ix == 0x7f800000)
198                 return zero;
199
200         a = y0f(x);
201         b = y1f(x);
202         /* quit if b is -inf */
203         GET_FLOAT_WORD(ib,b);
204         for (i = 1; i < n && ib != 0xff800000; i++){
205                 temp = b;
206                 b = ((float)(i+i)/x)*b - a;
207                 GET_FLOAT_WORD(ib, b);
208                 a = temp;
209         }
210         if (sign > 0)
211                 return b;
212         return -b;
213 }