home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / hobby / palmoon / palmoon.EXE / e_exp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-16  |  5.8 KB  |  177 lines

  1. // 15 August 1997, Rick Huebner:  Small changes made to adapt for MathLib
  2.  
  3. /* @(#)e_exp.c 5.1 93/09/24 */
  4. /*
  5.  * ====================================================
  6.  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
  7.  *
  8.  * Developed at SunPro, a Sun Microsystems, Inc. business.
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software is freely granted, provided that this notice 
  11.  * is preserved.
  12.  * ====================================================
  13.  */
  14.  
  15. #if defined(LIBM_SCCS) && !defined(lint)
  16. static char rcsid[] = "$NetBSD: e_exp.c,v 1.8 1995/05/10 20:45:03 jtc Exp $";
  17. #endif
  18.  
  19. /* __ieee754_exp(x)
  20.  * Returns the exponential of x.
  21.  *
  22.  * Method
  23.  *   1. Argument reduction:
  24.  *      Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658.
  25.  *    Given x, find r and integer k such that
  26.  *
  27.  *               x = k*ln2 + r,  |r| <= 0.5*ln2.  
  28.  *
  29.  *      Here r will be represented as r = hi-lo for better 
  30.  *    accuracy.
  31.  *
  32.  *   2. Approximation of exp(r) by a special rational function on
  33.  *    the interval [0,0.34658]:
  34.  *    Write
  35.  *        R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ...
  36.  *      We use a special Reme algorithm on [0,0.34658] to generate 
  37.  *     a polynomial of degree 5 to approximate R. The maximum error 
  38.  *    of this polynomial approximation is bounded by 2**-59. In
  39.  *    other words,
  40.  *        R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5
  41.  *      (where z=r*r, and the values of P1 to P5 are listed below)
  42.  *    and
  43.  *        |                  5          |     -59
  44.  *        | 2.0+P1*z+...+P5*z   -  R(z) | <= 2 
  45.  *        |                             |
  46.  *    The computation of exp(r) thus becomes
  47.  *                             2*r
  48.  *        exp(r) = 1 + -------
  49.  *                      R - r
  50.  *                                 r*R1(r)    
  51.  *               = 1 + r + ----------- (for better accuracy)
  52.  *                          2 - R1(r)
  53.  *    where
  54.  *                     2       4             10
  55.  *        R1(r) = r - (P1*r  + P2*r  + ... + P5*r   ).
  56.  *    
  57.  *   3. Scale back to obtain exp(x):
  58.  *    From step 1, we have
  59.  *       exp(x) = 2^k * exp(r)
  60.  *
  61.  * Special cases:
  62.  *    exp(INF) is INF, exp(NaN) is NaN;
  63.  *    exp(-INF) is 0, and
  64.  *    for finite argument, only exp(0)=1 is exact.
  65.  *
  66.  * Accuracy:
  67.  *    according to an error analysis, the error is always less than
  68.  *    1 ulp (unit in the last place).
  69.  *
  70.  * Misc. info.
  71.  *    For IEEE double 
  72.  *        if x >  7.09782712893383973096e+02 then exp(x) overflow
  73.  *        if x < -7.45133219101941108420e+02 then exp(x) underflow
  74.  *
  75.  * Constants:
  76.  * The hexadecimal values are the intended ones for the following 
  77.  * constants. The decimal values may be used, provided that the 
  78.  * compiler will convert from decimal to binary accurately enough
  79.  * to produce the hexadecimal values shown.
  80.  */
  81.  
  82. #include "math.h"
  83. #include "math_private.h"
  84.  
  85. #ifdef __STDC__
  86. static const double
  87. #else
  88. static double
  89. #endif
  90. one    = 1.0,
  91. //halF[2]    = {0.5,-0.5,},
  92. huge    = 1.0e+300,
  93. twom1000= 9.33263618503218878990e-302,     /* 2**-1000=0x01700000,0*/
  94. o_threshold=  7.09782712893383973096e+02,  /* 0x40862E42, 0xFEFA39EF */
  95. u_threshold= -7.45133219101941108420e+02,  /* 0xc0874910, 0xD52D3051 */
  96. //ln2HI[2]   ={ 6.93147180369123816490e-01,  /* 0x3fe62e42, 0xfee00000 */
  97. //         -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */
  98. //ln2LO[2]   ={ 1.90821492927058770002e-10,  /* 0x3dea39ef, 0x35793c76 */
  99. //         -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */
  100. invln2 =  1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */
  101. P1   =  1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */
  102. P2   = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */
  103. P3   =  6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */
  104. P4   = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */
  105. P5   =  4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */
  106.  
  107.  
  108. #ifdef __STDC__
  109.     double __ieee754_exp(double x)    /* default IEEE double exp */
  110. #else
  111.     double __ieee754_exp(x)    /* default IEEE double exp */
  112.     double x;
  113. #endif
  114. {
  115.     double halF[2], ln2HI[2], ln2LO[2];
  116.     double y,hi,lo,c,t;
  117.     int32_t k,xsb;
  118.     u_int32_t hx;
  119.  
  120.     halF[0] = 0.5; halF[1] = -0.5;
  121.     ln2HI[0] =  6.93147180369123816490e-01;  /* 0x3fe62e42, 0xfee00000 */
  122.     ln2HI[1] = -6.93147180369123816490e-01;  /* 0xbfe62e42, 0xfee00000 */
  123.     ln2LO[0] =  1.90821492927058770002e-10;  /* 0x3dea39ef, 0x35793c76 */
  124.     ln2LO[1] = -1.90821492927058770002e-10;  /* 0xbdea39ef, 0x35793c76 */
  125.     
  126.     GET_HIGH_WORD(hx,x);
  127.     xsb = (hx>>31)&1;        /* sign bit of x */
  128.     hx &= 0x7fffffff;        /* high word of |x| */
  129.  
  130.     /* filter out non-finite argument */
  131.     if(hx >= 0x40862E42) {            /* if |x|>=709.78... */
  132.             if(hx>=0x7ff00000) {
  133.             u_int32_t lx;
  134.         GET_LOW_WORD(lx,x);
  135.         if(((hx&0xfffff)|lx)!=0) 
  136.              return x+x;         /* NaN */
  137.         else return (xsb==0)? x:0.0;    /* exp(+-inf)={inf,0} */
  138.         }
  139.         if(x > o_threshold) return huge*huge; /* overflow */
  140.         if(x < u_threshold) return twom1000*twom1000; /* underflow */
  141.     }
  142.  
  143.     /* argument reduction */
  144.     if(hx > 0x3fd62e42) {        /* if  |x| > 0.5 ln2 */ 
  145.         if(hx < 0x3FF0A2B2) {    /* and |x| < 1.5 ln2 */
  146.         hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb;
  147.         } else {
  148.         k  = invln2*x+halF[xsb];
  149.         t  = k;
  150.         hi = x - t*ln2HI[0];    /* t*ln2HI is exact here */
  151.         lo = t*ln2LO[0];
  152.         }
  153.         x  = hi - lo;
  154.     } 
  155.     else if(hx < 0x3e300000)  {    /* when |x|<2**-28 */
  156.         if(huge+x>one) return one+x;/* trigger inexact */
  157.     }
  158.     else k = 0;
  159.  
  160.     /* x is now in primary range */
  161.     t  = x*x;
  162.     c  = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5))));
  163.     if(k==0)     return one-((x*c)/(c-2.0)-x); 
  164.     else         y = one-((lo-(x*c)/(2.0-c))-hi);
  165.     if(k >= -1021) {
  166.         u_int32_t hy;
  167.         GET_HIGH_WORD(hy,y);
  168.         SET_HIGH_WORD(y,hy+(k<<20));    /* add k to y's exponent */
  169.         return y;
  170.     } else {
  171.         u_int32_t hy;
  172.         GET_HIGH_WORD(hy,y);
  173.         SET_HIGH_WORD(y,hy+((k+1000)<<20));    /* add k to y's exponent */
  174.         return y*twom1000;
  175.     }
  176. }
  177.