base/math/e_exp.c

Go to the documentation of this file.
00001 /* @(#)e_exp.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 00013 #if defined(LIBM_SCCS) && !defined(lint) 00014 static char rcsid[] = "$NetBSD: e_exp.c,v 1.8 1995/05/10 20:45:03 jtc Exp $"; 00015 #endif 00016 00017 /* __ieee754_exp(x) 00018 * Returns the exponential of x. 00019 * 00020 * Method 00021 * 1. Argument reduction: 00022 * Reduce x to an r so that |r| <= 0.5*ln2 ~ 0.34658. 00023 * Given x, find r and integer k such that 00024 * 00025 * x = k*ln2 + r, |r| <= 0.5*ln2. 00026 * 00027 * Here r will be represented as r = hi-lo for better 00028 * accuracy. 00029 * 00030 * 2. Approximation of exp(r) by a special rational function on 00031 * the interval [0,0.34658]: 00032 * Write 00033 * R(r**2) = r*(exp(r)+1)/(exp(r)-1) = 2 + r*r/6 - r**4/360 + ... 00034 * We use a special Reme algorithm on [0,0.34658] to generate 00035 * a polynomial of degree 5 to approximate R. The maximum error 00036 * of this polynomial approximation is bounded by 2**-59. In 00037 * other words, 00038 * R(z) ~ 2.0 + P1*z + P2*z**2 + P3*z**3 + P4*z**4 + P5*z**5 00039 * (where z=r*r, and the values of P1 to P5 are listed below) 00040 * and 00041 * | 5 | -59 00042 * | 2.0+P1*z+...+P5*z - R(z) | <= 2 00043 * | | 00044 * The computation of exp(r) thus becomes 00045 * 2*r 00046 * exp(r) = 1 + ------- 00047 * R - r 00048 * r*R1(r) 00049 * = 1 + r + ----------- (for better accuracy) 00050 * 2 - R1(r) 00051 * where 00052 * 2 4 10 00053 * R1(r) = r - (P1*r + P2*r + ... + P5*r ). 00054 * 00055 * 3. Scale back to obtain exp(x): 00056 * From step 1, we have 00057 * exp(x) = 2^k * exp(r) 00058 * 00059 * Special cases: 00060 * exp(INF) is INF, exp(NaN) is NaN; 00061 * exp(-INF) is 0, and 00062 * for finite argument, only exp(0)=1 is exact. 00063 * 00064 * Accuracy: 00065 * according to an error analysis, the error is always less than 00066 * 1 ulp (unit in the last place). 00067 * 00068 * Misc. info. 00069 * For IEEE double 00070 * if x > 7.09782712893383973096e+02 then exp(x) overflow 00071 * if x < -7.45133219101941108420e+02 then exp(x) underflow 00072 * 00073 * Constants: 00074 * The hexadecimal values are the intended ones for the following 00075 * constants. The decimal values may be used, provided that the 00076 * compiler will convert from decimal to binary accurately enough 00077 * to produce the hexadecimal values shown. 00078 */ 00079 00080 #include "math.h" 00081 #include "mathP.h" 00082 00083 #ifdef __STDC__ 00084 static const double 00085 #else 00086 static double 00087 #endif 00088 one = 1.0, 00089 halF[2] = {0.5,-0.5,}, 00090 huge = 1.0e+300, 00091 twom1000= 9.33263618503218878990e-302, /* 2**-1000=0x01700000,0*/ 00092 o_threshold= 7.09782712893383973096e+02, /* 0x40862E42, 0xFEFA39EF */ 00093 u_threshold= -7.45133219101941108420e+02, /* 0xc0874910, 0xD52D3051 */ 00094 ln2HI[2] ={ 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */ 00095 -6.93147180369123816490e-01,},/* 0xbfe62e42, 0xfee00000 */ 00096 ln2LO[2] ={ 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */ 00097 -1.90821492927058770002e-10,},/* 0xbdea39ef, 0x35793c76 */ 00098 invln2 = 1.44269504088896338700e+00, /* 0x3ff71547, 0x652b82fe */ 00099 P1 = 1.66666666666666019037e-01, /* 0x3FC55555, 0x5555553E */ 00100 P2 = -2.77777777770155933842e-03, /* 0xBF66C16C, 0x16BEBD93 */ 00101 P3 = 6.61375632143793436117e-05, /* 0x3F11566A, 0xAF25DE2C */ 00102 P4 = -1.65339022054652515390e-06, /* 0xBEBBBD41, 0xC5D26BF1 */ 00103 P5 = 4.13813679705723846039e-08; /* 0x3E663769, 0x72BEA4D0 */ 00104 00105 00106 #ifdef __STDC__ 00107 double __ieee754_exp(double x) /* default IEEE double exp */ 00108 #else 00109 double __ieee754_exp(x) /* default IEEE double exp */ 00110 double x; 00111 #endif 00112 { 00113 double y,hi,lo,c,t; 00114 int32_t k,xsb; 00115 u_int32_t hx; 00116 00117 /* XXX hi, lo, k are not properly initialized. Bug? --ds */ 00118 hi = lo = k = 0; 00119 00120 GET_HIGH_WORD(hx,x); 00121 xsb = (hx>>31)&1; /* sign bit of x */ 00122 hx &= 0x7fffffff; /* high word of |x| */ 00123 00124 /* filter out non-finite argument */ 00125 if(hx >= 0x40862E42) { /* if |x|>=709.78... */ 00126 if(hx>=0x7ff00000) { 00127 u_int32_t lx; 00128 GET_LOW_WORD(lx,x); 00129 if(((hx&0xfffff)|lx)!=0) 00130 return x+x; /* NaN */ 00131 else return (xsb==0)? x:0.0; /* exp(+-inf)={inf,0} */ 00132 } 00133 if(x > o_threshold) return huge*huge; /* overflow */ 00134 if(x < u_threshold) return twom1000*twom1000; /* underflow */ 00135 } 00136 00137 /* argument reduction */ 00138 if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 00139 if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 00140 hi = x-ln2HI[xsb]; lo=ln2LO[xsb]; k = 1-xsb-xsb; 00141 } else { 00142 k = invln2*x+halF[xsb]; 00143 t = k; 00144 hi = x - t*ln2HI[0]; /* t*ln2HI is exact here */ 00145 lo = t*ln2LO[0]; 00146 } 00147 x = hi - lo; 00148 } 00149 else if(hx < 0x3e300000) { /* when |x|<2**-28 */ 00150 if(huge+x>one) return one+x;/* trigger inexact */ 00151 } 00152 else k = 0; 00153 00154 /* x is now in primary range */ 00155 t = x*x; 00156 c = x - t*(P1+t*(P2+t*(P3+t*(P4+t*P5)))); 00157 if(k==0) return one-((x*c)/(c-2.0)-x); 00158 else y = one-((lo-(x*c)/(2.0-c))-hi); 00159 if(k >= -1021) { 00160 u_int32_t hy; 00161 GET_HIGH_WORD(hy,y); 00162 SET_HIGH_WORD(y,hy+(k<<20)); /* add k to y's exponent */ 00163 return y; 00164 } else { 00165 u_int32_t hy; 00166 GET_HIGH_WORD(hy,y); 00167 SET_HIGH_WORD(y,hy+((k+1000)<<20)); /* add k to y's exponent */ 00168 return y*twom1000; 00169 } 00170 }

Generated on Thu Nov 20 11:49:51 2008 for RTAI API by doxygen 1.3.8