xref: /original-bsd/lib/libm/common_source/expm1.c (revision 6d57652c)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)expm1.c	5.6 (Berkeley) 10/09/90";
10 #endif /* not lint */
11 
12 /* EXPM1(X)
13  * RETURN THE EXPONENTIAL OF X MINUS ONE
14  * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS)
15  * CODED IN C BY K.C. NG, 1/19/85;
16  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85.
17  *
18  * Required system supported functions:
19  *	scalb(x,n)
20  *	copysign(x,y)
21  *	finite(x)
22  *
23  * Kernel function:
24  *	exp__E(x,c)
25  *
26  * Method:
27  *	1. Argument Reduction: given the input x, find r and integer k such
28  *	   that
29  *	                   x = k*ln2 + r,  |r| <= 0.5*ln2 .
30  *	   r will be represented as r := z+c for better accuracy.
31  *
32  *	2. Compute EXPM1(r)=exp(r)-1 by
33  *
34  *			EXPM1(r=z+c) := z + exp__E(z,c)
35  *
36  *	3. EXPM1(x) =  2^k * ( EXPM1(r) + 1-2^-k ).
37  *
38  * 	Remarks:
39  *	   1. When k=1 and z < -0.25, we use the following formula for
40  *	      better accuracy:
41  *			EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) )
42  *	   2. To avoid rounding error in 1-2^-k where k is large, we use
43  *			EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 }
44  *	      when k>56.
45  *
46  * Special cases:
47  *	EXPM1(INF) is INF, EXPM1(NaN) is NaN;
48  *	EXPM1(-INF)= -1;
49  *	for finite argument, only EXPM1(0)=0 is exact.
50  *
51  * Accuracy:
52  *	EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with
53  *	1,166,000 random arguments on a VAX, the maximum observed error was
54  *	.872 ulps (units of the last place).
55  *
56  * Constants:
57  * The hexadecimal values are the intended ones for the following constants.
58  * The decimal values may be used, provided that the compiler will convert
59  * from decimal to binary accurately enough to produce the hexadecimal values
60  * shown.
61  */
62 
63 #include "mathimpl.h"
64 
65 vc(ln2hi,  6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
66 vc(ln2lo,  1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
67 vc(lnhuge, 9.4961163736712506989E1   ,ec1d,43bd,9010,a73e,   7, .BDEC1DA73E9010)
68 vc(invln2, 1.4426950408889634148E0   ,aa3b,40b8,17f1,295c,   1, .B8AA3B295C17F1)
69 
70 ic(ln2hi,  6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
71 ic(ln2lo,  1.9082149292705877000E-10, -33, 1.A39EF35793C76)
72 ic(lnhuge, 7.1602103751842355450E2,     9, 1.6602B15B7ECF2)
73 ic(invln2, 1.4426950408889633870E0,     0, 1.71547652B82FE)
74 
75 #ifdef vccast
76 #define	ln2hi	vccast(ln2hi)
77 #define	ln2lo	vccast(ln2lo)
78 #define	lnhuge	vccast(lnhuge)
79 #define	invln2	vccast(invln2)
80 #endif
81 
82 double expm1(x)
83 double x;
84 {
85 	const static double one=1.0, half=1.0/2.0;
86 	double  z,hi,lo,c;
87 	int k;
88 #if defined(vax)||defined(tahoe)
89 	static prec=56;
90 #else	/* defined(vax)||defined(tahoe) */
91 	static prec=53;
92 #endif	/* defined(vax)||defined(tahoe) */
93 
94 #if !defined(vax)&&!defined(tahoe)
95 	if(x!=x) return(x);	/* x is NaN */
96 #endif	/* !defined(vax)&&!defined(tahoe) */
97 
98 	if( x <= lnhuge ) {
99 		if( x >= -40.0 ) {
100 
101 		    /* argument reduction : x - k*ln2 */
102 			k= invln2 *x+copysign(0.5,x);	/* k=NINT(x/ln2) */
103 			hi=x-k*ln2hi ;
104 			z=hi-(lo=k*ln2lo);
105 			c=(hi-z)-lo;
106 
107 			if(k==0) return(z+exp__E(z,c));
108 			if(k==1)
109 			    if(z< -0.25)
110 				{x=z+half;x +=exp__E(z,c); return(x+x);}
111 			    else
112 				{z+=exp__E(z,c); x=half+z; return(x+x);}
113 		    /* end of k=1 */
114 
115 			else {
116 			    if(k<=prec)
117 			      { x=one-scalb(one,-k); z += exp__E(z,c);}
118 			    else if(k<100)
119 			      { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;}
120 			    else
121 			      { x = exp__E(z,c)+z; z=one;}
122 
123 			    return (scalb(x+z,k));
124 			}
125 		}
126 		/* end of x > lnunfl */
127 
128 		else
129 		     /* expm1(-big#) rounded to -1 (inexact) */
130 		     if(finite(x))
131 			 { ln2hi+ln2lo; return(-one);}
132 
133 		     /* expm1(-INF) is -1 */
134 		     else return(-one);
135 	}
136 	/* end of x < lnhuge */
137 
138 	else
139 	/*  expm1(INF) is INF, expm1(+big#) overflows to INF */
140 	    return( finite(x) ?  scalb(one,5000) : x);
141 }
142