xref: /csrg-svn/lib/libm/common_source/expm1.c (revision 34124)
1*34124Sbostic /*
224595Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34124Sbostic  * All rights reserved.
4*34124Sbostic  *
5*34124Sbostic  * Redistribution and use in source and binary forms are permitted
6*34124Sbostic  * provided that this notice is preserved and that due credit is given
7*34124Sbostic  * to the University of California at Berkeley. The name of the University
8*34124Sbostic  * may not be used to endorse or promote products derived from this
9*34124Sbostic  * software without specific prior written permission. This software
10*34124Sbostic  * is provided ``as is'' without express or implied warranty.
11*34124Sbostic  *
12*34124Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34124Sbostic  * research project and hence should feel obligated to report their
14*34124Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34124Sbostic  * the sendbug(8) program, to the authors.
1624595Szliu  */
1724595Szliu 
1824595Szliu #ifndef lint
19*34124Sbostic static char sccsid[] = "@(#)expm1.c	5.2 (Berkeley) 04/29/88";
20*34124Sbostic #endif /* not lint */
2124595Szliu 
2224595Szliu /* EXPM1(X)
2324595Szliu  * RETURN THE EXPONENTIAL OF X MINUS ONE
2424595Szliu  * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS)
2524595Szliu  * CODED IN C BY K.C. NG, 1/19/85;
2624595Szliu  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85.
2724595Szliu  *
2824595Szliu  * Required system supported functions:
2924595Szliu  *	scalb(x,n)
3024595Szliu  *	copysign(x,y)
3124595Szliu  *	finite(x)
3224595Szliu  *
3324595Szliu  * Kernel function:
3424595Szliu  *	exp__E(x,c)
3524595Szliu  *
3624595Szliu  * Method:
3724595Szliu  *	1. Argument Reduction: given the input x, find r and integer k such
3824595Szliu  *	   that
3924595Szliu  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
4024595Szliu  *	   r will be represented as r := z+c for better accuracy.
4124595Szliu  *
4224595Szliu  *	2. Compute EXPM1(r)=exp(r)-1 by
4324595Szliu  *
4424595Szliu  *			EXPM1(r=z+c) := z + exp__E(z,c)
4524595Szliu  *
4624595Szliu  *	3. EXPM1(x) =  2^k * ( EXPM1(r) + 1-2^-k ).
4724595Szliu  *
4824595Szliu  * 	Remarks:
4924595Szliu  *	   1. When k=1 and z < -0.25, we use the following formula for
5024595Szliu  *	      better accuracy:
5124595Szliu  *			EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) )
5224595Szliu  *	   2. To avoid rounding error in 1-2^-k where k is large, we use
5324595Szliu  *			EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 }
5424595Szliu  *	      when k>56.
5524595Szliu  *
5624595Szliu  * Special cases:
5724595Szliu  *	EXPM1(INF) is INF, EXPM1(NaN) is NaN;
5824595Szliu  *	EXPM1(-INF)= -1;
5924595Szliu  *	for finite argument, only EXPM1(0)=0 is exact.
6024595Szliu  *
6124595Szliu  * Accuracy:
6224595Szliu  *	EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with
6324595Szliu  *	1,166,000 random arguments on a VAX, the maximum observed error was
6424595Szliu  *	.872 ulps (units of the last place).
6524595Szliu  *
6624595Szliu  * Constants:
6724595Szliu  * The hexadecimal values are the intended ones for the following constants.
6824595Szliu  * The decimal values may be used, provided that the compiler will convert
6924595Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7024595Szliu  * shown.
7124595Szliu  */
7224595Szliu 
7331853Szliu #if defined(vax)||defined(tahoe)	/* VAX D format */
7431853Szliu #ifdef vax
7531812Szliu #define _0x(A,B)	0x/**/A/**/B
7631853Szliu #else	/* vax */
7731812Szliu #define _0x(A,B)	0x/**/B/**/A
7831853Szliu #endif	/* vax */
7926893Selefunt /* static double */
8024595Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
8124595Szliu /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
8224595Szliu /* lnhuge =  9.4961163736712506989E1     , Hex  2^  7   *  .BDEC1DA73E9010 */
8324595Szliu /* invln2 =  1.4426950408889634148E0     ; Hex  2^  1   *  .B8AA3B295C17F1 */
8431812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
8531812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
8631812Szliu static long    lnhugex[] = { _0x(ec1d,43bd), _0x(9010,a73e)};
8731812Szliu static long    invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)};
8824595Szliu #define    ln2hi    (*(double*)ln2hix)
8924595Szliu #define    ln2lo    (*(double*)ln2lox)
9024595Szliu #define   lnhuge    (*(double*)lnhugex)
9124595Szliu #define   invln2    (*(double*)invln2x)
9231853Szliu #else	/* defined(vax)||defined(tahoe)	*/
9326893Selefunt static double
9424595Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
9524595Szliu ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
9624595Szliu lnhuge =  7.1602103751842355450E2     , /*Hex  2^  9   *  1.6602B15B7ECF2 */
9724595Szliu invln2 =  1.4426950408889633870E0     ; /*Hex  2^  0   *  1.71547652B82FE */
9831853Szliu #endif	/* defined(vax)||defined(tahoe)	*/
9924595Szliu 
10024595Szliu double expm1(x)
10124595Szliu double x;
10224595Szliu {
10326893Selefunt 	static double one=1.0, half=1.0/2.0;
10424595Szliu 	double scalb(), copysign(), exp__E(), z,hi,lo,c;
10524595Szliu 	int k,finite();
10631853Szliu #if defined(vax)||defined(tahoe)
10724595Szliu 	static prec=56;
10831853Szliu #else	/* defined(vax)||defined(tahoe) */
10924595Szliu 	static prec=53;
11031853Szliu #endif	/* defined(vax)||defined(tahoe) */
11131853Szliu #if !defined(vax)&&!defined(tahoe)
11224595Szliu 	if(x!=x) return(x);	/* x is NaN */
11331853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
11424595Szliu 
11524595Szliu 	if( x <= lnhuge ) {
11624595Szliu 		if( x >= -40.0 ) {
11724595Szliu 
11824595Szliu 		    /* argument reduction : x - k*ln2 */
11924595Szliu 			k= invln2 *x+copysign(0.5,x);	/* k=NINT(x/ln2) */
12024595Szliu 			hi=x-k*ln2hi ;
12124595Szliu 			z=hi-(lo=k*ln2lo);
12224595Szliu 			c=(hi-z)-lo;
12324595Szliu 
12424595Szliu 			if(k==0) return(z+exp__E(z,c));
12524595Szliu 			if(k==1)
12624595Szliu 			    if(z< -0.25)
12724595Szliu 				{x=z+half;x +=exp__E(z,c); return(x+x);}
12824595Szliu 			    else
12924595Szliu 				{z+=exp__E(z,c); x=half+z; return(x+x);}
13024595Szliu 		    /* end of k=1 */
13124595Szliu 
13224595Szliu 			else {
13324595Szliu 			    if(k<=prec)
13424595Szliu 			      { x=one-scalb(one,-k); z += exp__E(z,c);}
13524595Szliu 			    else if(k<100)
13624595Szliu 			      { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;}
13724595Szliu 			    else
13824595Szliu 			      { x = exp__E(z,c)+z; z=one;}
13924595Szliu 
14024595Szliu 			    return (scalb(x+z,k));
14124595Szliu 			}
14224595Szliu 		}
14324595Szliu 		/* end of x > lnunfl */
14424595Szliu 
14524595Szliu 		else
14624595Szliu 		     /* expm1(-big#) rounded to -1 (inexact) */
14724595Szliu 		     if(finite(x))
14824595Szliu 			 { ln2hi+ln2lo; return(-one);}
14924595Szliu 
15024595Szliu 		     /* expm1(-INF) is -1 */
15124595Szliu 		     else return(-one);
15224595Szliu 	}
15324595Szliu 	/* end of x < lnhuge */
15424595Szliu 
15524595Szliu 	else
15624595Szliu 	/*  expm1(INF) is INF, expm1(+big#) overflows to INF */
15724595Szliu 	    return( finite(x) ?  scalb(one,5000) : x);
15824595Szliu }
159