Back to index

glibc  2.9
s_expm1.c
Go to the documentation of this file.
00001 /* @(#)s_expm1.c 5.1 93/09/24 */
00002 /*
00003  * ====================================================
00004  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00005  *
00006  * Developed at SunPro, a Sun Microsystems, Inc. business.
00007  * Permission to use, copy, modify, and distribute this
00008  * software is freely granted, provided that this notice
00009  * is preserved.
00010  * ====================================================
00011  */
00012 /* Modified by Naohiko Shimizu/Tokai University, Japan 1997/08/25,
00013    for performance improvement on pipelined processors.
00014 */
00015 
00016 #if defined(LIBM_SCCS) && !defined(lint)
00017 static char rcsid[] = "$NetBSD: s_expm1.c,v 1.8 1995/05/10 20:47:09 jtc Exp $";
00018 #endif
00019 
00020 /* expm1(x)
00021  * Returns exp(x)-1, the exponential of x minus 1.
00022  *
00023  * Method
00024  *   1. Argument reduction:
00025  *     Given x, find r and integer k such that
00026  *
00027  *               x = k*ln2 + r,  |r| <= 0.5*ln2 ~ 0.34658
00028  *
00029  *      Here a correction term c will be computed to compensate
00030  *     the error in r when rounded to a floating-point number.
00031  *
00032  *   2. Approximating expm1(r) by a special rational function on
00033  *     the interval [0,0.34658]:
00034  *     Since
00035  *         r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ...
00036  *     we define R1(r*r) by
00037  *         r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r)
00038  *     That is,
00039  *         R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r)
00040  *                 = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r))
00041  *                 = 1 - r^2/60 + r^4/2520 - r^6/100800 + ...
00042  *      We use a special Reme algorithm on [0,0.347] to generate
00043  *     a polynomial of degree 5 in r*r to approximate R1. The
00044  *     maximum error of this polynomial approximation is bounded
00045  *     by 2**-61. In other words,
00046  *         R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5
00047  *     where  Q1  =  -1.6666666666666567384E-2,
00048  *            Q2  =   3.9682539681370365873E-4,
00049  *            Q3  =  -9.9206344733435987357E-6,
00050  *            Q4  =   2.5051361420808517002E-7,
00051  *            Q5  =  -6.2843505682382617102E-9;
00052  *     (where z=r*r, and the values of Q1 to Q5 are listed below)
00053  *     with error bounded by
00054  *         |                  5           |     -61
00055  *         | 1.0+Q1*z+...+Q5*z   -  R1(z) | <= 2
00056  *         |                              |
00057  *
00058  *     expm1(r) = exp(r)-1 is then computed by the following
00059  *     specific way which minimize the accumulation rounding error:
00060  *                          2     3
00061  *                         r     r    [ 3 - (R1 + R1*r/2)  ]
00062  *           expm1(r) = r + --- + --- * [--------------------]
00063  *                          2     2    [ 6 - r*(3 - R1*r/2) ]
00064  *
00065  *     To compensate the error in the argument reduction, we use
00066  *            expm1(r+c) = expm1(r) + c + expm1(r)*c
00067  *                      ~ expm1(r) + c + r*c
00068  *     Thus c+r*c will be added in as the correction terms for
00069  *     expm1(r+c). Now rearrange the term to avoid optimization
00070  *     screw up:
00071  *                    (      2                                    2 )
00072  *                    ({  ( r    [ R1 -  (3 - R1*r/2) ]  )  }    r  )
00073  *      expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- )
00074  *                     ({  ( 2    [ 6 - r*(3 - R1*r/2) ]  )  }    2  )
00075  *                      (                                             )
00076  *
00077  *               = r - E
00078  *   3. Scale back to obtain expm1(x):
00079  *     From step 1, we have
00080  *        expm1(x) = either 2^k*[expm1(r)+1] - 1
00081  *                = or     2^k*[expm1(r) + (1-2^-k)]
00082  *   4. Implementation notes:
00083  *     (A). To save one multiplication, we scale the coefficient Qi
00084  *          to Qi*2^i, and replace z by (x^2)/2.
00085  *     (B). To achieve maximum accuracy, we compute expm1(x) by
00086  *       (i)   if x < -56*ln2, return -1.0, (raise inexact if x!=inf)
00087  *       (ii)  if k=0, return r-E
00088  *       (iii) if k=-1, return 0.5*(r-E)-0.5
00089  *        (iv)       if k=1 if r < -0.25, return 2*((r+0.5)- E)
00090  *                          else        return  1.0+2.0*(r-E);
00091  *       (v)   if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1)
00092  *       (vi)  if k <= 20, return 2^k((1-2^-k)-(E-r)), else
00093  *       (vii) return 2^k(1-((E+2^-k)-r))
00094  *
00095  * Special cases:
00096  *     expm1(INF) is INF, expm1(NaN) is NaN;
00097  *     expm1(-INF) is -1, and
00098  *     for finite argument, only expm1(0)=0 is exact.
00099  *
00100  * Accuracy:
00101  *     according to an error analysis, the error is always less than
00102  *     1 ulp (unit in the last place).
00103  *
00104  * Misc. info.
00105  *     For IEEE double
00106  *         if x >  7.09782712893383973096e+02 then expm1(x) overflow
00107  *
00108  * Constants:
00109  * The hexadecimal values are the intended ones for the following
00110  * constants. The decimal values may be used, provided that the
00111  * compiler will convert from decimal to binary accurately enough
00112  * to produce the hexadecimal values shown.
00113  */
00114 
00115 #include "math.h"
00116 #include "math_private.h"
00117 #define one Q[0]
00118 #ifdef __STDC__
00119 static const double
00120 #else
00121 static double
00122 #endif
00123 huge          = 1.0e+300,
00124 tiny          = 1.0e-300,
00125 o_threshold   = 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */
00126 ln2_hi        = 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */
00127 ln2_lo        = 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */
00128 invln2        = 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */
00129        /* scaled coefficients related to expm1 */
00130 Q[]  =  {1.0, -3.33333333333331316428e-02, /* BFA11111 111110F4 */
00131    1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */
00132   -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */
00133    4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */
00134   -2.01099218183624371326e-07}; /* BE8AFDB7 6E09C32D */
00135 
00136 #ifdef __STDC__
00137        double __expm1(double x)
00138 #else
00139        double __expm1(x)
00140        double x;
00141 #endif
00142 {
00143        double y,hi,lo,c,t,e,hxs,hfx,r1,h2,h4,R1,R2,R3;
00144        int32_t k,xsb;
00145        u_int32_t hx;
00146 
00147        GET_HIGH_WORD(hx,x);
00148        xsb = hx&0x80000000;        /* sign bit of x */
00149        if(xsb==0) y=x; else y= -x; /* y = |x| */
00150        hx &= 0x7fffffff;           /* high word of |x| */
00151 
00152     /* filter out huge and non-finite argument */
00153        if(hx >= 0x4043687A) {                    /* if |x|>=56*ln2 */
00154            if(hx >= 0x40862E42) {         /* if |x|>=709.78... */
00155                 if(hx>=0x7ff00000) {
00156                   u_int32_t low;
00157                   GET_LOW_WORD(low,x);
00158                   if(((hx&0xfffff)|low)!=0)
00159                        return x+x;         /* NaN */
00160                   else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */
00161                }
00162                if(x > o_threshold) return huge*huge; /* overflow */
00163            }
00164            if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */
00165               if(x+tiny<0.0)              /* raise inexact */
00166               return tiny-one;     /* return -1 */
00167            }
00168        }
00169 
00170     /* argument reduction */
00171        if(hx > 0x3fd62e42) {              /* if  |x| > 0.5 ln2 */
00172            if(hx < 0x3FF0A2B2) {   /* and |x| < 1.5 ln2 */
00173               if(xsb==0)
00174                   {hi = x - ln2_hi; lo =  ln2_lo;  k =  1;}
00175               else
00176                   {hi = x + ln2_hi; lo = -ln2_lo;  k = -1;}
00177            } else {
00178               k  = invln2*x+((xsb==0)?0.5:-0.5);
00179               t  = k;
00180               hi = x - t*ln2_hi;   /* t*ln2_hi is exact here */
00181               lo = t*ln2_lo;
00182            }
00183            x  = hi - lo;
00184            c  = (hi-x)-lo;
00185        }
00186        else if(hx < 0x3c900000) {         /* when |x|<2**-54, return x */
00187            t = huge+x;      /* return x with inexact flags when x!=0 */
00188            return x - (t-(huge+x));
00189        }
00190        else k = 0;
00191 
00192     /* x is now in primary range */
00193        hfx = 0.5*x;
00194        hxs = x*hfx;
00195 #ifdef DO_NOT_USE_THIS
00196        r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5))));
00197 #else
00198        R1 = one+hxs*Q[1]; h2 = hxs*hxs;
00199        R2 = Q[2]+hxs*Q[3]; h4 = h2*h2;
00200        R3 = Q[4]+hxs*Q[5];
00201        r1 = R1 + h2*R2 + h4*R3;
00202 #endif
00203        t  = 3.0-r1*hfx;
00204        e  = hxs*((r1-t)/(6.0 - x*t));
00205        if(k==0) return x - (x*e-hxs);            /* c is 0 */
00206        else {
00207            e  = (x*(e-c)-c);
00208            e -= hxs;
00209            if(k== -1) return 0.5*(x-e)-0.5;
00210            if(k==1) {
00211                      if(x < -0.25) return -2.0*(e-(x+0.5));
00212                      else         return  one+2.0*(x-e);
00213            }
00214            if (k <= -2 || k>56) {   /* suffice to return exp(x)-1 */
00215                u_int32_t high;
00216                y = one-(e-x);
00217               GET_HIGH_WORD(high,y);
00218               SET_HIGH_WORD(y,high+(k<<20));     /* add k to y's exponent */
00219                return y-one;
00220            }
00221            t = one;
00222            if(k<20) {
00223                u_int32_t high;
00224                SET_HIGH_WORD(t,0x3ff00000 - (0x200000>>k));  /* t=1-2^-k */
00225                      y = t-(e-x);
00226               GET_HIGH_WORD(high,y);
00227               SET_HIGH_WORD(y,high+(k<<20));     /* add k to y's exponent */
00228           } else {
00229                u_int32_t high;
00230               SET_HIGH_WORD(t,((0x3ff-k)<<20));  /* 2^-k */
00231                      y = x-(e+t);
00232                      y += one;
00233               GET_HIGH_WORD(high,y);
00234               SET_HIGH_WORD(y,high+(k<<20));     /* add k to y's exponent */
00235            }
00236        }
00237        return y;
00238 }
00239 weak_alias (__expm1, expm1)
00240 #ifdef NO_LONG_DOUBLE
00241 strong_alias (__expm1, __expm1l)
00242 weak_alias (__expm1, expm1l)
00243 #endif