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

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