1*37da2899SCharles.Forsyth /* derived from /netlib/fdlibm */ 2*37da2899SCharles.Forsyth 3*37da2899SCharles.Forsyth /* @(#)s_expm1.c 1.3 95/01/18 */ 4*37da2899SCharles.Forsyth /* 5*37da2899SCharles.Forsyth * ==================================================== 6*37da2899SCharles.Forsyth * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 7*37da2899SCharles.Forsyth * 8*37da2899SCharles.Forsyth * Developed at SunSoft, a Sun Microsystems, Inc. business. 9*37da2899SCharles.Forsyth * Permission to use, copy, modify, and distribute this 10*37da2899SCharles.Forsyth * software is freely granted, provided that this notice 11*37da2899SCharles.Forsyth * is preserved. 12*37da2899SCharles.Forsyth * ==================================================== 13*37da2899SCharles.Forsyth */ 14*37da2899SCharles.Forsyth 15*37da2899SCharles.Forsyth /* expm1(x) 16*37da2899SCharles.Forsyth * Returns exp(x)-1, the exponential of x minus 1. 17*37da2899SCharles.Forsyth * 18*37da2899SCharles.Forsyth * Method 19*37da2899SCharles.Forsyth * 1. Argument reduction: 20*37da2899SCharles.Forsyth * Given x, find r and integer k such that 21*37da2899SCharles.Forsyth * 22*37da2899SCharles.Forsyth * x = k*ln2 + r, |r| <= 0.5*ln2 ~ 0.34658 23*37da2899SCharles.Forsyth * 24*37da2899SCharles.Forsyth * Here a correction term c will be computed to compensate 25*37da2899SCharles.Forsyth * the error in r when rounded to a floating-point number. 26*37da2899SCharles.Forsyth * 27*37da2899SCharles.Forsyth * 2. Approximating expm1(r) by a special rational function on 28*37da2899SCharles.Forsyth * the interval [0,0.34658]: 29*37da2899SCharles.Forsyth * Since 30*37da2899SCharles.Forsyth * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 - r^4/360 + ... 31*37da2899SCharles.Forsyth * we define R1(r*r) by 32*37da2899SCharles.Forsyth * r*(exp(r)+1)/(exp(r)-1) = 2+ r^2/6 * R1(r*r) 33*37da2899SCharles.Forsyth * That is, 34*37da2899SCharles.Forsyth * R1(r**2) = 6/r *((exp(r)+1)/(exp(r)-1) - 2/r) 35*37da2899SCharles.Forsyth * = 6/r * ( 1 + 2.0*(1/(exp(r)-1) - 1/r)) 36*37da2899SCharles.Forsyth * = 1 - r^2/60 + r^4/2520 - r^6/100800 + ... 37*37da2899SCharles.Forsyth * We use a special Reme algorithm on [0,0.347] to generate 38*37da2899SCharles.Forsyth * a polynomial of degree 5 in r*r to approximate R1. The 39*37da2899SCharles.Forsyth * maximum error of this polynomial approximation is bounded 40*37da2899SCharles.Forsyth * by 2**-61. In other words, 41*37da2899SCharles.Forsyth * R1(z) ~ 1.0 + Q1*z + Q2*z**2 + Q3*z**3 + Q4*z**4 + Q5*z**5 42*37da2899SCharles.Forsyth * where Q1 = -1.6666666666666567384E-2, 43*37da2899SCharles.Forsyth * Q2 = 3.9682539681370365873E-4, 44*37da2899SCharles.Forsyth * Q3 = -9.9206344733435987357E-6, 45*37da2899SCharles.Forsyth * Q4 = 2.5051361420808517002E-7, 46*37da2899SCharles.Forsyth * Q5 = -6.2843505682382617102E-9; 47*37da2899SCharles.Forsyth * (where z=r*r, and the values of Q1 to Q5 are listed below) 48*37da2899SCharles.Forsyth * with error bounded by 49*37da2899SCharles.Forsyth * | 5 | -61 50*37da2899SCharles.Forsyth * | 1.0+Q1*z+...+Q5*z - R1(z) | <= 2 51*37da2899SCharles.Forsyth * | | 52*37da2899SCharles.Forsyth * 53*37da2899SCharles.Forsyth * expm1(r) = exp(r)-1 is then computed by the following 54*37da2899SCharles.Forsyth * specific way which minimize the accumulation rounding error: 55*37da2899SCharles.Forsyth * 2 3 56*37da2899SCharles.Forsyth * r r [ 3 - (R1 + R1*r/2) ] 57*37da2899SCharles.Forsyth * expm1(r) = r + --- + --- * [--------------------] 58*37da2899SCharles.Forsyth * 2 2 [ 6 - r*(3 - R1*r/2) ] 59*37da2899SCharles.Forsyth * 60*37da2899SCharles.Forsyth * To compensate the error in the argument reduction, we use 61*37da2899SCharles.Forsyth * expm1(r+c) = expm1(r) + c + expm1(r)*c 62*37da2899SCharles.Forsyth * ~ expm1(r) + c + r*c 63*37da2899SCharles.Forsyth * Thus c+r*c will be added in as the correction terms for 64*37da2899SCharles.Forsyth * expm1(r+c). Now rearrange the term to avoid optimization 65*37da2899SCharles.Forsyth * screw up: 66*37da2899SCharles.Forsyth * ( 2 2 ) 67*37da2899SCharles.Forsyth * ({ ( r [ R1 - (3 - R1*r/2) ] ) } r ) 68*37da2899SCharles.Forsyth * expm1(r+c)~r - ({r*(--- * [--------------------]-c)-c} - --- ) 69*37da2899SCharles.Forsyth * ({ ( 2 [ 6 - r*(3 - R1*r/2) ] ) } 2 ) 70*37da2899SCharles.Forsyth * ( ) 71*37da2899SCharles.Forsyth * 72*37da2899SCharles.Forsyth * = r - E 73*37da2899SCharles.Forsyth * 3. Scale back to obtain expm1(x): 74*37da2899SCharles.Forsyth * From step 1, we have 75*37da2899SCharles.Forsyth * expm1(x) = either 2^k*[expm1(r)+1] - 1 76*37da2899SCharles.Forsyth * = or 2^k*[expm1(r) + (1-2^-k)] 77*37da2899SCharles.Forsyth * 4. Implementation notes: 78*37da2899SCharles.Forsyth * (A). To save one multiplication, we scale the coefficient Qi 79*37da2899SCharles.Forsyth * to Qi*2^i, and replace z by (x^2)/2. 80*37da2899SCharles.Forsyth * (B). To achieve maximum accuracy, we compute expm1(x) by 81*37da2899SCharles.Forsyth * (i) if x < -56*ln2, return -1.0, (raise inexact if x!=inf) 82*37da2899SCharles.Forsyth * (ii) if k=0, return r-E 83*37da2899SCharles.Forsyth * (iii) if k=-1, return 0.5*(r-E)-0.5 84*37da2899SCharles.Forsyth * (iv) if k=1 if r < -0.25, return 2*((r+0.5)- E) 85*37da2899SCharles.Forsyth * else return 1.0+2.0*(r-E); 86*37da2899SCharles.Forsyth * (v) if (k<-2||k>56) return 2^k(1-(E-r)) - 1 (or exp(x)-1) 87*37da2899SCharles.Forsyth * (vi) if k <= 20, return 2^k((1-2^-k)-(E-r)), else 88*37da2899SCharles.Forsyth * (vii) return 2^k(1-((E+2^-k)-r)) 89*37da2899SCharles.Forsyth * 90*37da2899SCharles.Forsyth * Special cases: 91*37da2899SCharles.Forsyth * expm1(INF) is INF, expm1(NaN) is NaN; 92*37da2899SCharles.Forsyth * expm1(-INF) is -1, and 93*37da2899SCharles.Forsyth * for finite argument, only expm1(0)=0 is exact. 94*37da2899SCharles.Forsyth * 95*37da2899SCharles.Forsyth * Accuracy: 96*37da2899SCharles.Forsyth * according to an error analysis, the error is always less than 97*37da2899SCharles.Forsyth * 1 ulp (unit in the last place). 98*37da2899SCharles.Forsyth * 99*37da2899SCharles.Forsyth * Misc. info. 100*37da2899SCharles.Forsyth * For IEEE double 101*37da2899SCharles.Forsyth * if x > 7.09782712893383973096e+02 then expm1(x) overflow 102*37da2899SCharles.Forsyth * 103*37da2899SCharles.Forsyth * Constants: 104*37da2899SCharles.Forsyth * The hexadecimal values are the intended ones for the following 105*37da2899SCharles.Forsyth * constants. The decimal values may be used, provided that the 106*37da2899SCharles.Forsyth * compiler will convert from decimal to binary accurately enough 107*37da2899SCharles.Forsyth * to produce the hexadecimal values shown. 108*37da2899SCharles.Forsyth */ 109*37da2899SCharles.Forsyth 110*37da2899SCharles.Forsyth #include "fdlibm.h" 111*37da2899SCharles.Forsyth 112*37da2899SCharles.Forsyth static const double 113*37da2899SCharles.Forsyth one = 1.0, 114*37da2899SCharles.Forsyth Huge = 1.0e+300, 115*37da2899SCharles.Forsyth tiny = 1.0e-300, 116*37da2899SCharles.Forsyth o_threshold = 7.09782712893383973096e+02,/* 0x40862E42, 0xFEFA39EF */ 117*37da2899SCharles.Forsyth ln2_hi = 6.93147180369123816490e-01,/* 0x3fe62e42, 0xfee00000 */ 118*37da2899SCharles.Forsyth ln2_lo = 1.90821492927058770002e-10,/* 0x3dea39ef, 0x35793c76 */ 119*37da2899SCharles.Forsyth invln2 = 1.44269504088896338700e+00,/* 0x3ff71547, 0x652b82fe */ 120*37da2899SCharles.Forsyth /* scaled coefficients related to expm1 */ 121*37da2899SCharles.Forsyth Q1 = -3.33333333333331316428e-02, /* BFA11111 111110F4 */ 122*37da2899SCharles.Forsyth Q2 = 1.58730158725481460165e-03, /* 3F5A01A0 19FE5585 */ 123*37da2899SCharles.Forsyth Q3 = -7.93650757867487942473e-05, /* BF14CE19 9EAADBB7 */ 124*37da2899SCharles.Forsyth Q4 = 4.00821782732936239552e-06, /* 3ED0CFCA 86E65239 */ 125*37da2899SCharles.Forsyth Q5 = -2.01099218183624371326e-07; /* BE8AFDB7 6E09C32D */ 126*37da2899SCharles.Forsyth expm1(double x)127*37da2899SCharles.Forsyth double expm1(double x) 128*37da2899SCharles.Forsyth { 129*37da2899SCharles.Forsyth double y,hi,lo,c,t,e,hxs,hfx,r1; 130*37da2899SCharles.Forsyth int k,xsb; 131*37da2899SCharles.Forsyth unsigned hx; 132*37da2899SCharles.Forsyth 133*37da2899SCharles.Forsyth hx = __HI(x); /* high word of x */ 134*37da2899SCharles.Forsyth xsb = hx&0x80000000; /* sign bit of x */ 135*37da2899SCharles.Forsyth if(xsb==0) y=x; else y= -x; /* y = |x| */ 136*37da2899SCharles.Forsyth hx &= 0x7fffffff; /* high word of |x| */ 137*37da2899SCharles.Forsyth 138*37da2899SCharles.Forsyth /* filter out Huge and non-finite argument */ 139*37da2899SCharles.Forsyth if(hx >= 0x4043687A) { /* if |x|>=56*ln2 */ 140*37da2899SCharles.Forsyth if(hx >= 0x40862E42) { /* if |x|>=709.78... */ 141*37da2899SCharles.Forsyth if(hx>=0x7ff00000) { 142*37da2899SCharles.Forsyth if(((hx&0xfffff)|__LO(x))!=0) 143*37da2899SCharles.Forsyth return x+x; /* NaN */ 144*37da2899SCharles.Forsyth else return (xsb==0)? x:-1.0;/* exp(+-inf)={inf,-1} */ 145*37da2899SCharles.Forsyth } 146*37da2899SCharles.Forsyth if(x > o_threshold) return Huge*Huge; /* overflow */ 147*37da2899SCharles.Forsyth } 148*37da2899SCharles.Forsyth if(xsb!=0) { /* x < -56*ln2, return -1.0 with inexact */ 149*37da2899SCharles.Forsyth if(x+tiny<0.0) /* raise inexact */ 150*37da2899SCharles.Forsyth return tiny-one; /* return -1 */ 151*37da2899SCharles.Forsyth } 152*37da2899SCharles.Forsyth } 153*37da2899SCharles.Forsyth 154*37da2899SCharles.Forsyth /* argument reduction */ 155*37da2899SCharles.Forsyth if(hx > 0x3fd62e42) { /* if |x| > 0.5 ln2 */ 156*37da2899SCharles.Forsyth if(hx < 0x3FF0A2B2) { /* and |x| < 1.5 ln2 */ 157*37da2899SCharles.Forsyth if(xsb==0) 158*37da2899SCharles.Forsyth {hi = x - ln2_hi; lo = ln2_lo; k = 1;} 159*37da2899SCharles.Forsyth else 160*37da2899SCharles.Forsyth {hi = x + ln2_hi; lo = -ln2_lo; k = -1;} 161*37da2899SCharles.Forsyth } else { 162*37da2899SCharles.Forsyth k = invln2*x+((xsb==0)?0.5:-0.5); 163*37da2899SCharles.Forsyth t = k; 164*37da2899SCharles.Forsyth hi = x - t*ln2_hi; /* t*ln2_hi is exact here */ 165*37da2899SCharles.Forsyth lo = t*ln2_lo; 166*37da2899SCharles.Forsyth } 167*37da2899SCharles.Forsyth x = hi - lo; 168*37da2899SCharles.Forsyth c = (hi-x)-lo; 169*37da2899SCharles.Forsyth } 170*37da2899SCharles.Forsyth else if(hx < 0x3c900000) { /* when |x|<2**-54, return x */ 171*37da2899SCharles.Forsyth t = Huge+x; /* return x with inexact flags when x!=0 */ 172*37da2899SCharles.Forsyth return x - (t-(Huge+x)); 173*37da2899SCharles.Forsyth } 174*37da2899SCharles.Forsyth else k = 0; 175*37da2899SCharles.Forsyth 176*37da2899SCharles.Forsyth /* x is now in primary range */ 177*37da2899SCharles.Forsyth hfx = 0.5*x; 178*37da2899SCharles.Forsyth hxs = x*hfx; 179*37da2899SCharles.Forsyth r1 = one+hxs*(Q1+hxs*(Q2+hxs*(Q3+hxs*(Q4+hxs*Q5)))); 180*37da2899SCharles.Forsyth t = 3.0-r1*hfx; 181*37da2899SCharles.Forsyth e = hxs*((r1-t)/(6.0 - x*t)); 182*37da2899SCharles.Forsyth if(k==0) return x - (x*e-hxs); /* c is 0 */ 183*37da2899SCharles.Forsyth else { 184*37da2899SCharles.Forsyth e = (x*(e-c)-c); 185*37da2899SCharles.Forsyth e -= hxs; 186*37da2899SCharles.Forsyth if(k== -1) return 0.5*(x-e)-0.5; 187*37da2899SCharles.Forsyth if(k==1) 188*37da2899SCharles.Forsyth if(x < -0.25) return -2.0*(e-(x+0.5)); 189*37da2899SCharles.Forsyth else return one+2.0*(x-e); 190*37da2899SCharles.Forsyth if (k <= -2 || k>56) { /* suffice to return exp(x)-1 */ 191*37da2899SCharles.Forsyth y = one-(e-x); 192*37da2899SCharles.Forsyth __HI(y) += (k<<20); /* add k to y's exponent */ 193*37da2899SCharles.Forsyth return y-one; 194*37da2899SCharles.Forsyth } 195*37da2899SCharles.Forsyth t = one; 196*37da2899SCharles.Forsyth if(k<20) { 197*37da2899SCharles.Forsyth __HI(t) = 0x3ff00000 - (0x200000>>k); /* t=1-2^-k */ 198*37da2899SCharles.Forsyth y = t-(e-x); 199*37da2899SCharles.Forsyth __HI(y) += (k<<20); /* add k to y's exponent */ 200*37da2899SCharles.Forsyth } else { 201*37da2899SCharles.Forsyth __HI(t) = ((0x3ff-k)<<20); /* 2^-k */ 202*37da2899SCharles.Forsyth y = x-(e+t); 203*37da2899SCharles.Forsyth y += one; 204*37da2899SCharles.Forsyth __HI(y) += (k<<20); /* add k to y's exponent */ 205*37da2899SCharles.Forsyth } 206*37da2899SCharles.Forsyth } 207*37da2899SCharles.Forsyth return y; 208*37da2899SCharles.Forsyth } 209