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