xref: /csrg-svn/lib/libm/common_source/expm1.c (revision 35679)
134124Sbostic /*
224595Szliu  * Copyright (c) 1985 Regents of the University of California.
334124Sbostic  * All rights reserved.
434124Sbostic  *
534124Sbostic  * Redistribution and use in source and binary forms are permitted
634931Sbostic  * provided that the above copyright notice and this paragraph are
734931Sbostic  * duplicated in all such forms and that any documentation,
834931Sbostic  * advertising materials, and other materials related to such
934931Sbostic  * distribution and use acknowledge that the software was developed
1034931Sbostic  * by the University of California, Berkeley.  The name of the
1134931Sbostic  * University may not be used to endorse or promote products derived
1234931Sbostic  * from this software without specific prior written permission.
1334931Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434931Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534931Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1634124Sbostic  *
1734124Sbostic  * All recipients should regard themselves as participants in an ongoing
1834124Sbostic  * research project and hence should feel obligated to report their
1934124Sbostic  * experiences (good or bad) with these elementary function codes, using
2034124Sbostic  * the sendbug(8) program, to the authors.
2124595Szliu  */
2224595Szliu 
2324595Szliu #ifndef lint
24*35679Sbostic static char sccsid[] = "@(#)expm1.c	5.4 (Berkeley) 09/22/88";
2534124Sbostic #endif /* not lint */
2624595Szliu 
2724595Szliu /* EXPM1(X)
2824595Szliu  * RETURN THE EXPONENTIAL OF X MINUS ONE
2924595Szliu  * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS)
3024595Szliu  * CODED IN C BY K.C. NG, 1/19/85;
3124595Szliu  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85.
3224595Szliu  *
3324595Szliu  * Required system supported functions:
3424595Szliu  *	scalb(x,n)
3524595Szliu  *	copysign(x,y)
3624595Szliu  *	finite(x)
3724595Szliu  *
3824595Szliu  * Kernel function:
3924595Szliu  *	exp__E(x,c)
4024595Szliu  *
4124595Szliu  * Method:
4224595Szliu  *	1. Argument Reduction: given the input x, find r and integer k such
4324595Szliu  *	   that
4424595Szliu  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
4524595Szliu  *	   r will be represented as r := z+c for better accuracy.
4624595Szliu  *
4724595Szliu  *	2. Compute EXPM1(r)=exp(r)-1 by
4824595Szliu  *
4924595Szliu  *			EXPM1(r=z+c) := z + exp__E(z,c)
5024595Szliu  *
5124595Szliu  *	3. EXPM1(x) =  2^k * ( EXPM1(r) + 1-2^-k ).
5224595Szliu  *
5324595Szliu  * 	Remarks:
5424595Szliu  *	   1. When k=1 and z < -0.25, we use the following formula for
5524595Szliu  *	      better accuracy:
5624595Szliu  *			EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) )
5724595Szliu  *	   2. To avoid rounding error in 1-2^-k where k is large, we use
5824595Szliu  *			EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 }
5924595Szliu  *	      when k>56.
6024595Szliu  *
6124595Szliu  * Special cases:
6224595Szliu  *	EXPM1(INF) is INF, EXPM1(NaN) is NaN;
6324595Szliu  *	EXPM1(-INF)= -1;
6424595Szliu  *	for finite argument, only EXPM1(0)=0 is exact.
6524595Szliu  *
6624595Szliu  * Accuracy:
6724595Szliu  *	EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with
6824595Szliu  *	1,166,000 random arguments on a VAX, the maximum observed error was
6924595Szliu  *	.872 ulps (units of the last place).
7024595Szliu  *
7124595Szliu  * Constants:
7224595Szliu  * The hexadecimal values are the intended ones for the following constants.
7324595Szliu  * The decimal values may be used, provided that the compiler will convert
7424595Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7524595Szliu  * shown.
7624595Szliu  */
7724595Szliu 
78*35679Sbostic #include "mathimpl.h"
7924595Szliu 
80*35679Sbostic vc(ln2hi,  6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
81*35679Sbostic vc(ln2lo,  1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
82*35679Sbostic vc(lnhuge, 9.4961163736712506989E1   ,ec1d,43bd,9010,a73e,   7, .BDEC1DA73E9010)
83*35679Sbostic vc(invln2, 1.4426950408889634148E0   ,aa3b,40b8,17f1,295c,   1, .B8AA3B295C17F1)
84*35679Sbostic 
85*35679Sbostic ic(ln2hi,  6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
86*35679Sbostic ic(ln2lo,  1.9082149292705877000E-10, -33, 1.A39EF35793C76)
87*35679Sbostic ic(lnhuge, 7.1602103751842355450E2,     9, 1.6602B15B7ECF2)
88*35679Sbostic ic(invln2, 1.4426950408889633870E0,     0, 1.71547652B82FE)
89*35679Sbostic 
90*35679Sbostic #ifdef vccast
91*35679Sbostic #define	ln2hi	vccast(ln2hi)
92*35679Sbostic #define	ln2lo	vccast(ln2lo)
93*35679Sbostic #define	lnhuge	vccast(lnhuge)
94*35679Sbostic #define	invln2	vccast(invln2)
95*35679Sbostic #endif
96*35679Sbostic 
9724595Szliu double expm1(x)
9824595Szliu double x;
9924595Szliu {
100*35679Sbostic 	const static double one=1.0, half=1.0/2.0;
101*35679Sbostic 	double  z,hi,lo,c;
102*35679Sbostic 	int k;
10331853Szliu #if defined(vax)||defined(tahoe)
10424595Szliu 	static prec=56;
10531853Szliu #else	/* defined(vax)||defined(tahoe) */
10624595Szliu 	static prec=53;
10731853Szliu #endif	/* defined(vax)||defined(tahoe) */
108*35679Sbostic 
10931853Szliu #if !defined(vax)&&!defined(tahoe)
11024595Szliu 	if(x!=x) return(x);	/* x is NaN */
11131853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
11224595Szliu 
11324595Szliu 	if( x <= lnhuge ) {
11424595Szliu 		if( x >= -40.0 ) {
11524595Szliu 
11624595Szliu 		    /* argument reduction : x - k*ln2 */
11724595Szliu 			k= invln2 *x+copysign(0.5,x);	/* k=NINT(x/ln2) */
11824595Szliu 			hi=x-k*ln2hi ;
11924595Szliu 			z=hi-(lo=k*ln2lo);
12024595Szliu 			c=(hi-z)-lo;
12124595Szliu 
12224595Szliu 			if(k==0) return(z+exp__E(z,c));
12324595Szliu 			if(k==1)
12424595Szliu 			    if(z< -0.25)
12524595Szliu 				{x=z+half;x +=exp__E(z,c); return(x+x);}
12624595Szliu 			    else
12724595Szliu 				{z+=exp__E(z,c); x=half+z; return(x+x);}
12824595Szliu 		    /* end of k=1 */
12924595Szliu 
13024595Szliu 			else {
13124595Szliu 			    if(k<=prec)
13224595Szliu 			      { x=one-scalb(one,-k); z += exp__E(z,c);}
13324595Szliu 			    else if(k<100)
13424595Szliu 			      { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;}
13524595Szliu 			    else
13624595Szliu 			      { x = exp__E(z,c)+z; z=one;}
13724595Szliu 
13824595Szliu 			    return (scalb(x+z,k));
13924595Szliu 			}
14024595Szliu 		}
14124595Szliu 		/* end of x > lnunfl */
14224595Szliu 
14324595Szliu 		else
14424595Szliu 		     /* expm1(-big#) rounded to -1 (inexact) */
14524595Szliu 		     if(finite(x))
14624595Szliu 			 { ln2hi+ln2lo; return(-one);}
14724595Szliu 
14824595Szliu 		     /* expm1(-INF) is -1 */
14924595Szliu 		     else return(-one);
15024595Szliu 	}
15124595Szliu 	/* end of x < lnhuge */
15224595Szliu 
15324595Szliu 	else
15424595Szliu 	/*  expm1(INF) is INF, expm1(+big#) overflows to INF */
15524595Szliu 	    return( finite(x) ?  scalb(one,5000) : x);
15624595Szliu }
157