xref: /csrg-svn/lib/libm/common_source/expm1.c (revision 42657)
134124Sbostic /*
224595Szliu  * Copyright (c) 1985 Regents of the University of California.
334124Sbostic  * All rights reserved.
434124Sbostic  *
5*42657Sbostic  * %sccs.include.redist.c%
634124Sbostic  *
734124Sbostic  * All recipients should regard themselves as participants in an ongoing
834124Sbostic  * research project and hence should feel obligated to report their
934124Sbostic  * experiences (good or bad) with these elementary function codes, using
1034124Sbostic  * the sendbug(8) program, to the authors.
1124595Szliu  */
1224595Szliu 
1324595Szliu #ifndef lint
14*42657Sbostic static char sccsid[] = "@(#)expm1.c	5.5 (Berkeley) 06/01/90";
1534124Sbostic #endif /* not lint */
1624595Szliu 
1724595Szliu /* EXPM1(X)
1824595Szliu  * RETURN THE EXPONENTIAL OF X MINUS ONE
1924595Szliu  * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS)
2024595Szliu  * CODED IN C BY K.C. NG, 1/19/85;
2124595Szliu  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85.
2224595Szliu  *
2324595Szliu  * Required system supported functions:
2424595Szliu  *	scalb(x,n)
2524595Szliu  *	copysign(x,y)
2624595Szliu  *	finite(x)
2724595Szliu  *
2824595Szliu  * Kernel function:
2924595Szliu  *	exp__E(x,c)
3024595Szliu  *
3124595Szliu  * Method:
3224595Szliu  *	1. Argument Reduction: given the input x, find r and integer k such
3324595Szliu  *	   that
3424595Szliu  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
3524595Szliu  *	   r will be represented as r := z+c for better accuracy.
3624595Szliu  *
3724595Szliu  *	2. Compute EXPM1(r)=exp(r)-1 by
3824595Szliu  *
3924595Szliu  *			EXPM1(r=z+c) := z + exp__E(z,c)
4024595Szliu  *
4124595Szliu  *	3. EXPM1(x) =  2^k * ( EXPM1(r) + 1-2^-k ).
4224595Szliu  *
4324595Szliu  * 	Remarks:
4424595Szliu  *	   1. When k=1 and z < -0.25, we use the following formula for
4524595Szliu  *	      better accuracy:
4624595Szliu  *			EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) )
4724595Szliu  *	   2. To avoid rounding error in 1-2^-k where k is large, we use
4824595Szliu  *			EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 }
4924595Szliu  *	      when k>56.
5024595Szliu  *
5124595Szliu  * Special cases:
5224595Szliu  *	EXPM1(INF) is INF, EXPM1(NaN) is NaN;
5324595Szliu  *	EXPM1(-INF)= -1;
5424595Szliu  *	for finite argument, only EXPM1(0)=0 is exact.
5524595Szliu  *
5624595Szliu  * Accuracy:
5724595Szliu  *	EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with
5824595Szliu  *	1,166,000 random arguments on a VAX, the maximum observed error was
5924595Szliu  *	.872 ulps (units of the last place).
6024595Szliu  *
6124595Szliu  * Constants:
6224595Szliu  * The hexadecimal values are the intended ones for the following constants.
6324595Szliu  * The decimal values may be used, provided that the compiler will convert
6424595Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6524595Szliu  * shown.
6624595Szliu  */
6724595Szliu 
6835679Sbostic #include "mathimpl.h"
6924595Szliu 
7035679Sbostic vc(ln2hi,  6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
7135679Sbostic vc(ln2lo,  1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
7235679Sbostic vc(lnhuge, 9.4961163736712506989E1   ,ec1d,43bd,9010,a73e,   7, .BDEC1DA73E9010)
7335679Sbostic vc(invln2, 1.4426950408889634148E0   ,aa3b,40b8,17f1,295c,   1, .B8AA3B295C17F1)
7435679Sbostic 
7535679Sbostic ic(ln2hi,  6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
7635679Sbostic ic(ln2lo,  1.9082149292705877000E-10, -33, 1.A39EF35793C76)
7735679Sbostic ic(lnhuge, 7.1602103751842355450E2,     9, 1.6602B15B7ECF2)
7835679Sbostic ic(invln2, 1.4426950408889633870E0,     0, 1.71547652B82FE)
7935679Sbostic 
8035679Sbostic #ifdef vccast
8135679Sbostic #define	ln2hi	vccast(ln2hi)
8235679Sbostic #define	ln2lo	vccast(ln2lo)
8335679Sbostic #define	lnhuge	vccast(lnhuge)
8435679Sbostic #define	invln2	vccast(invln2)
8535679Sbostic #endif
8635679Sbostic 
8724595Szliu double expm1(x)
8824595Szliu double x;
8924595Szliu {
9035679Sbostic 	const static double one=1.0, half=1.0/2.0;
9135679Sbostic 	double  z,hi,lo,c;
9235679Sbostic 	int k;
9331853Szliu #if defined(vax)||defined(tahoe)
9424595Szliu 	static prec=56;
9531853Szliu #else	/* defined(vax)||defined(tahoe) */
9624595Szliu 	static prec=53;
9731853Szliu #endif	/* defined(vax)||defined(tahoe) */
9835679Sbostic 
9931853Szliu #if !defined(vax)&&!defined(tahoe)
10024595Szliu 	if(x!=x) return(x);	/* x is NaN */
10131853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
10224595Szliu 
10324595Szliu 	if( x <= lnhuge ) {
10424595Szliu 		if( x >= -40.0 ) {
10524595Szliu 
10624595Szliu 		    /* argument reduction : x - k*ln2 */
10724595Szliu 			k= invln2 *x+copysign(0.5,x);	/* k=NINT(x/ln2) */
10824595Szliu 			hi=x-k*ln2hi ;
10924595Szliu 			z=hi-(lo=k*ln2lo);
11024595Szliu 			c=(hi-z)-lo;
11124595Szliu 
11224595Szliu 			if(k==0) return(z+exp__E(z,c));
11324595Szliu 			if(k==1)
11424595Szliu 			    if(z< -0.25)
11524595Szliu 				{x=z+half;x +=exp__E(z,c); return(x+x);}
11624595Szliu 			    else
11724595Szliu 				{z+=exp__E(z,c); x=half+z; return(x+x);}
11824595Szliu 		    /* end of k=1 */
11924595Szliu 
12024595Szliu 			else {
12124595Szliu 			    if(k<=prec)
12224595Szliu 			      { x=one-scalb(one,-k); z += exp__E(z,c);}
12324595Szliu 			    else if(k<100)
12424595Szliu 			      { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;}
12524595Szliu 			    else
12624595Szliu 			      { x = exp__E(z,c)+z; z=one;}
12724595Szliu 
12824595Szliu 			    return (scalb(x+z,k));
12924595Szliu 			}
13024595Szliu 		}
13124595Szliu 		/* end of x > lnunfl */
13224595Szliu 
13324595Szliu 		else
13424595Szliu 		     /* expm1(-big#) rounded to -1 (inexact) */
13524595Szliu 		     if(finite(x))
13624595Szliu 			 { ln2hi+ln2lo; return(-one);}
13724595Szliu 
13824595Szliu 		     /* expm1(-INF) is -1 */
13924595Szliu 		     else return(-one);
14024595Szliu 	}
14124595Szliu 	/* end of x < lnhuge */
14224595Szliu 
14324595Szliu 	else
14424595Szliu 	/*  expm1(INF) is INF, expm1(+big#) overflows to INF */
14524595Szliu 	    return( finite(x) ?  scalb(one,5000) : x);
14624595Szliu }
147