xref: /csrg-svn/lib/libm/common_source/expm1.c (revision 61285)
134124Sbostic /*
2*61285Sbostic  * Copyright (c) 1985, 1993
3*61285Sbostic  *	The Regents of the University of California.  All rights reserved.
434124Sbostic  *
542657Sbostic  * %sccs.include.redist.c%
624595Szliu  */
724595Szliu 
824595Szliu #ifndef lint
9*61285Sbostic static char sccsid[] = "@(#)expm1.c	8.1 (Berkeley) 06/04/93";
1034124Sbostic #endif /* not lint */
1124595Szliu 
1224595Szliu /* EXPM1(X)
1324595Szliu  * RETURN THE EXPONENTIAL OF X MINUS ONE
1424595Szliu  * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS)
1524595Szliu  * CODED IN C BY K.C. NG, 1/19/85;
1624595Szliu  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85.
1724595Szliu  *
1824595Szliu  * Required system supported functions:
1924595Szliu  *	scalb(x,n)
2024595Szliu  *	copysign(x,y)
2124595Szliu  *	finite(x)
2224595Szliu  *
2324595Szliu  * Kernel function:
2424595Szliu  *	exp__E(x,c)
2524595Szliu  *
2624595Szliu  * Method:
2724595Szliu  *	1. Argument Reduction: given the input x, find r and integer k such
2824595Szliu  *	   that
2924595Szliu  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
3024595Szliu  *	   r will be represented as r := z+c for better accuracy.
3124595Szliu  *
3224595Szliu  *	2. Compute EXPM1(r)=exp(r)-1 by
3324595Szliu  *
3424595Szliu  *			EXPM1(r=z+c) := z + exp__E(z,c)
3524595Szliu  *
3624595Szliu  *	3. EXPM1(x) =  2^k * ( EXPM1(r) + 1-2^-k ).
3724595Szliu  *
3824595Szliu  * 	Remarks:
3924595Szliu  *	   1. When k=1 and z < -0.25, we use the following formula for
4024595Szliu  *	      better accuracy:
4124595Szliu  *			EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) )
4224595Szliu  *	   2. To avoid rounding error in 1-2^-k where k is large, we use
4324595Szliu  *			EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 }
4424595Szliu  *	      when k>56.
4524595Szliu  *
4624595Szliu  * Special cases:
4724595Szliu  *	EXPM1(INF) is INF, EXPM1(NaN) is NaN;
4824595Szliu  *	EXPM1(-INF)= -1;
4924595Szliu  *	for finite argument, only EXPM1(0)=0 is exact.
5024595Szliu  *
5124595Szliu  * Accuracy:
5224595Szliu  *	EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with
5324595Szliu  *	1,166,000 random arguments on a VAX, the maximum observed error was
5424595Szliu  *	.872 ulps (units of the last place).
5524595Szliu  *
5624595Szliu  * Constants:
5724595Szliu  * The hexadecimal values are the intended ones for the following constants.
5824595Szliu  * The decimal values may be used, provided that the compiler will convert
5924595Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
6024595Szliu  * shown.
6124595Szliu  */
6224595Szliu 
6335679Sbostic #include "mathimpl.h"
6424595Szliu 
6535679Sbostic vc(ln2hi,  6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
6635679Sbostic vc(ln2lo,  1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
6735679Sbostic vc(lnhuge, 9.4961163736712506989E1   ,ec1d,43bd,9010,a73e,   7, .BDEC1DA73E9010)
6835679Sbostic vc(invln2, 1.4426950408889634148E0   ,aa3b,40b8,17f1,295c,   1, .B8AA3B295C17F1)
6935679Sbostic 
7035679Sbostic ic(ln2hi,  6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
7135679Sbostic ic(ln2lo,  1.9082149292705877000E-10, -33, 1.A39EF35793C76)
7235679Sbostic ic(lnhuge, 7.1602103751842355450E2,     9, 1.6602B15B7ECF2)
7335679Sbostic ic(invln2, 1.4426950408889633870E0,     0, 1.71547652B82FE)
7435679Sbostic 
7535679Sbostic #ifdef vccast
7635679Sbostic #define	ln2hi	vccast(ln2hi)
7735679Sbostic #define	ln2lo	vccast(ln2lo)
7835679Sbostic #define	lnhuge	vccast(lnhuge)
7935679Sbostic #define	invln2	vccast(invln2)
8035679Sbostic #endif
8135679Sbostic 
8224595Szliu double expm1(x)
8324595Szliu double x;
8424595Szliu {
8535679Sbostic 	const static double one=1.0, half=1.0/2.0;
8635679Sbostic 	double  z,hi,lo,c;
8735679Sbostic 	int k;
8831853Szliu #if defined(vax)||defined(tahoe)
8924595Szliu 	static prec=56;
9031853Szliu #else	/* defined(vax)||defined(tahoe) */
9124595Szliu 	static prec=53;
9231853Szliu #endif	/* defined(vax)||defined(tahoe) */
9335679Sbostic 
9431853Szliu #if !defined(vax)&&!defined(tahoe)
9524595Szliu 	if(x!=x) return(x);	/* x is NaN */
9631853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
9724595Szliu 
9824595Szliu 	if( x <= lnhuge ) {
9924595Szliu 		if( x >= -40.0 ) {
10024595Szliu 
10124595Szliu 		    /* argument reduction : x - k*ln2 */
10224595Szliu 			k= invln2 *x+copysign(0.5,x);	/* k=NINT(x/ln2) */
10324595Szliu 			hi=x-k*ln2hi ;
10424595Szliu 			z=hi-(lo=k*ln2lo);
10524595Szliu 			c=(hi-z)-lo;
10624595Szliu 
10757452Sbostic 			if(k==0) return(z+__exp__E(z,c));
10824595Szliu 			if(k==1)
10924595Szliu 			    if(z< -0.25)
11057452Sbostic 				{x=z+half;x +=__exp__E(z,c); return(x+x);}
11124595Szliu 			    else
11257452Sbostic 				{z+=__exp__E(z,c); x=half+z; return(x+x);}
11324595Szliu 		    /* end of k=1 */
11424595Szliu 
11524595Szliu 			else {
11624595Szliu 			    if(k<=prec)
11757452Sbostic 			      { x=one-scalb(one,-k); z += __exp__E(z,c);}
11824595Szliu 			    else if(k<100)
11957452Sbostic 			      { x = __exp__E(z,c)-scalb(one,-k); x+=z; z=one;}
12024595Szliu 			    else
12157452Sbostic 			      { x = __exp__E(z,c)+z; z=one;}
12224595Szliu 
12324595Szliu 			    return (scalb(x+z,k));
12424595Szliu 			}
12524595Szliu 		}
12624595Szliu 		/* end of x > lnunfl */
12724595Szliu 
12824595Szliu 		else
12924595Szliu 		     /* expm1(-big#) rounded to -1 (inexact) */
13024595Szliu 		     if(finite(x))
13124595Szliu 			 { ln2hi+ln2lo; return(-one);}
13224595Szliu 
13324595Szliu 		     /* expm1(-INF) is -1 */
13424595Szliu 		     else return(-one);
13524595Szliu 	}
13624595Szliu 	/* end of x < lnhuge */
13724595Szliu 
13824595Szliu 	else
13924595Szliu 	/*  expm1(INF) is INF, expm1(+big#) overflows to INF */
14024595Szliu 	    return( finite(x) ?  scalb(one,5000) : x);
14124595Szliu }
142