Back to index

glibc  2.9
e_jnf.c
Go to the documentation of this file.
00001 /* e_jnf.c -- float version of e_jn.c.
00002  * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
00003  */
00004 
00005 /*
00006  * ====================================================
00007  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00008  *
00009  * Developed at SunPro, a Sun Microsystems, Inc. business.
00010  * Permission to use, copy, modify, and distribute this
00011  * software is freely granted, provided that this notice
00012  * is preserved.
00013  * ====================================================
00014  */
00015 
00016 #if defined(LIBM_SCCS) && !defined(lint)
00017 static char rcsid[] = "$NetBSD: e_jnf.c,v 1.5 1995/05/10 20:45:37 jtc Exp $";
00018 #endif
00019 
00020 #include "math.h"
00021 #include "math_private.h"
00022 
00023 #ifdef __STDC__
00024 static const float
00025 #else
00026 static float
00027 #endif
00028 two   =  2.0000000000e+00, /* 0x40000000 */
00029 one   =  1.0000000000e+00; /* 0x3F800000 */
00030 
00031 #ifdef __STDC__
00032 static const float zero  =  0.0000000000e+00;
00033 #else
00034 static float zero  =  0.0000000000e+00;
00035 #endif
00036 
00037 #ifdef __STDC__
00038        float __ieee754_jnf(int n, float x)
00039 #else
00040        float __ieee754_jnf(n,x)
00041        int n; float x;
00042 #endif
00043 {
00044        int32_t i,hx,ix, sgn;
00045        float a, b, temp, di;
00046        float z, w;
00047 
00048     /* J(-n,x) = (-1)^n * J(n, x), J(n, -x) = (-1)^n * J(n, x)
00049      * Thus, J(-n,x) = J(n,-x)
00050      */
00051        GET_FLOAT_WORD(hx,x);
00052        ix = 0x7fffffff&hx;
00053     /* if J(n,NaN) is NaN */
00054        if(ix>0x7f800000) return x+x;
00055        if(n<0){
00056               n = -n;
00057               x = -x;
00058               hx ^= 0x80000000;
00059        }
00060        if(n==0) return(__ieee754_j0f(x));
00061        if(n==1) return(__ieee754_j1f(x));
00062        sgn = (n&1)&(hx>>31);       /* even n -- 0, odd n -- sign(x) */
00063        x = fabsf(x);
00064        if(ix==0||ix>=0x7f800000)   /* if x is 0 or inf */
00065            b = zero;
00066        else if((float)n<=x) {
00067               /* Safe to use J(n+1,x)=2n/x *J(n,x)-J(n-1,x) */
00068            a = __ieee754_j0f(x);
00069            b = __ieee754_j1f(x);
00070            for(i=1;i<n;i++){
00071               temp = b;
00072               b = b*((float)(i+i)/x) - a; /* avoid underflow */
00073               a = temp;
00074            }
00075        } else {
00076            if(ix<0x30800000) {     /* x < 2**-29 */
00077     /* x is tiny, return the first Taylor expansion of J(n,x)
00078      * J(n,x) = 1/n!*(x/2)^n  - ...
00079      */
00080               if(n>33)      /* underflow */
00081                   b = zero;
00082               else {
00083                   temp = x*(float)0.5; b = temp;
00084                   for (a=one,i=2;i<=n;i++) {
00085                      a *= (float)i;              /* a = n! */
00086                      b *= temp;           /* b = (x/2)^n */
00087                   }
00088                   b = b/a;
00089               }
00090            } else {
00091               /* use backward recurrence */
00092               /*                   x      x^2      x^2
00093                *  J(n,x)/J(n-1,x) =  ----   ------   ------   .....
00094                *                   2n  - 2(n+1) - 2(n+2)
00095                *
00096                *                   1      1        1
00097                *  (for large x)   =  ----  ------   ------   .....
00098                *                   2n   2(n+1)   2(n+2)
00099                *                   -- - ------ - ------ -
00100                *                    x     x         x
00101                *
00102                * Let w = 2n/x and h=2/x, then the above quotient
00103                * is equal to the continued fraction:
00104                *                1
00105                *     = -----------------------
00106                *                   1
00107                *        w - -----------------
00108                *                     1
00109                *             w+h - ---------
00110                *                   w+2h - ...
00111                *
00112                * To determine how many terms needed, let
00113                * Q(0) = w, Q(1) = w(w+h) - 1,
00114                * Q(k) = (w+k*h)*Q(k-1) - Q(k-2),
00115                * When Q(k) > 1e4   good for single
00116                * When Q(k) > 1e9   good for double
00117                * When Q(k) > 1e17  good for quadruple
00118                */
00119            /* determine k */
00120               float t,v;
00121               float q0,q1,h,tmp; int32_t k,m;
00122               w  = (n+n)/(float)x; h = (float)2.0/(float)x;
00123               q0 = w;  z = w+h; q1 = w*z - (float)1.0; k=1;
00124               while(q1<(float)1.0e9) {
00125                      k += 1; z += h;
00126                      tmp = z*q1 - q0;
00127                      q0 = q1;
00128                      q1 = tmp;
00129               }
00130               m = n+n;
00131               for(t=zero, i = 2*(n+k); i>=m; i -= 2) t = one/(i/x-t);
00132               a = t;
00133               b = one;
00134               /*  estimate log((2/x)^n*n!) = n*log(2/x)+n*ln(n)
00135                *  Hence, if n*(log(2n/x)) > ...
00136                *  single 8.8722839355e+01
00137                *  double 7.09782712893383973096e+02
00138                *  long double 1.1356523406294143949491931077970765006170e+04
00139                *  then recurrent value may overflow and the result is
00140                *  likely underflow to zero
00141                */
00142               tmp = n;
00143               v = two/x;
00144               tmp = tmp*__ieee754_logf(fabsf(v*tmp));
00145               if(tmp<(float)8.8721679688e+01) {
00146                   for(i=n-1,di=(float)(i+i);i>0;i--){
00147                       temp = b;
00148                      b *= di;
00149                      b  = b/x - a;
00150                       a = temp;
00151                      di -= two;
00152                   }
00153               } else {
00154                   for(i=n-1,di=(float)(i+i);i>0;i--){
00155                       temp = b;
00156                      b *= di;
00157                      b  = b/x - a;
00158                       a = temp;
00159                      di -= two;
00160                   /* scale b to avoid spurious overflow */
00161                      if(b>(float)1e10) {
00162                          a /= b;
00163                          t /= b;
00164                          b  = one;
00165                      }
00166                   }
00167               }
00168               b = (t*__ieee754_j0f(x)/b);
00169            }
00170        }
00171        if(sgn==1) return -b; else return b;
00172 }
00173 
00174 #ifdef __STDC__
00175        float __ieee754_ynf(int n, float x)
00176 #else
00177        float __ieee754_ynf(n,x)
00178        int n; float x;
00179 #endif
00180 {
00181        int32_t i,hx,ix;
00182        u_int32_t ib;
00183        int32_t sign;
00184        float a, b, temp;
00185 
00186        GET_FLOAT_WORD(hx,x);
00187        ix = 0x7fffffff&hx;
00188     /* if Y(n,NaN) is NaN */
00189        if(ix>0x7f800000) return x+x;
00190        if(ix==0) return -HUGE_VALF+x;  /* -inf and overflow exception.  */
00191        if(hx<0) return zero/(zero*x);
00192        sign = 1;
00193        if(n<0){
00194               n = -n;
00195               sign = 1 - ((n&1)<<1);
00196        }
00197        if(n==0) return(__ieee754_y0f(x));
00198        if(n==1) return(sign*__ieee754_y1f(x));
00199        if(ix==0x7f800000) return zero;
00200 
00201        a = __ieee754_y0f(x);
00202        b = __ieee754_y1f(x);
00203        /* quit if b is -inf */
00204        GET_FLOAT_WORD(ib,b);
00205        for(i=1;i<n&&ib!=0xff800000;i++){
00206            temp = b;
00207            b = ((float)(i+i)/x)*b - a;
00208            GET_FLOAT_WORD(ib,b);
00209            a = temp;
00210        }
00211        if(sign>0) return b; else return -b;
00212 }