1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * All recipients should regard themselves as participants in an ongoing
18  * research project and hence should feel obligated to report their
19  * experiences (good or bad) with these elementary function codes, using
20  * the sendbug(8) program, to the authors.
21  */
22 
23 #ifndef lint
24 static char sccsid[] = "@(#)exp__E.c	5.4 (Berkeley) 09/22/88";
25 #endif /* not lint */
26 
27 /* exp__E(x,c)
28  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
29  * (c is the correction term for x)
30  * exp__E RETURNS
31  *
32  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
33  *       exp__E(x,c) = 	|
34  *			 \  0 ,  |x| < 1E-19.
35  *
36  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
37  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
38  * CODED IN C BY K.C. NG, 1/31/85;
39  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
40  *
41  * Required system supported function:
42  *	copysign(x,y)
43  *
44  * Method:
45  *	1. Rational approximation. Let r=x+c.
46  *	   Based on
47  *                                   2 * sinh(r/2)
48  *                exp(r) - 1 =   ----------------------   ,
49  *                               cosh(r/2) - sinh(r/2)
50  *	   exp__E(r) is computed using
51  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
52  *                   --- + (c + x*[---------------------------------- + c ])
53  *                    2                          1 - W
54  * 	   where  P := p1*x^2 + p2*x^4,
55  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
56  *	          W := x/2-(Q-x*P),
57  *
58  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
59  *	    nomials P and Q may be regarded as the approximations to sinh
60  *	    and cosh :
61  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
62  *
63  *         The coefficients were obtained by a special Remez algorithm.
64  *
65  * Approximation error:
66  *
67  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
68  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
69  *   |	     x			           |	    2**(-69).  (VAX D)
70  *
71  * Constants:
72  * The hexadecimal values are the intended ones for the following constants.
73  * The decimal values may be used, provided that the compiler will convert
74  * from decimal to binary accurately enough to produce the hexadecimal values
75  * shown.
76  */
77 
78 #include "mathimpl.h"
79 
80 vc(p1, 1.5150724356786683059E-2 ,3abe,3d78,066a,67e1,  -6, .F83ABE67E1066A)
81 vc(p2, 6.3112487873718332688E-5 ,5b42,3984,0173,48cd, -13, .845B4248CD0173)
82 vc(q1, 1.1363478204690669916E-1 ,b95a,3ee8,ec45,44a2,  -3, .E8B95A44A2EC45)
83 vc(q2, 1.2624568129896839182E-3 ,7905,3ba5,f5e7,72e4,  -9, .A5790572E4F5E7)
84 vc(q3, 1.5021856115869022674E-6 ,9eb4,36c9,c395,604a, -19, .C99EB4604AC395)
85 
86 ic(p1, 1.3887401997267371720E-2,  -7, 1.C70FF8B3CC2CF)
87 ic(p2, 3.3044019718331897649E-5, -15, 1.15317DF4526C4)
88 ic(q1, 1.1110813732786649355E-1,  -4, 1.C719538248597)
89 ic(q2, 9.9176615021572857300E-4, -10, 1.03FC4CB8C98E8)
90 
91 #ifdef vccast
92 #define       p1    vccast(p1)
93 #define       p2    vccast(p2)
94 #define       q1    vccast(q1)
95 #define       q2    vccast(q2)
96 #define       q3    vccast(q3)
97 #endif
98 
99 double exp__E(x,c)
100 double x,c;
101 {
102 	const static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
103 	double z,p,q,xp,xh,w;
104 	if(copysign(x,one)>small) {
105            z = x*x  ;
106 	   p = z*( p1 +z* p2 );
107 #if defined(vax)||defined(tahoe)
108            q = z*( q1 +z*( q2 +z* q3 ));
109 #else	/* defined(vax)||defined(tahoe) */
110            q = z*( q1 +z*  q2 );
111 #endif	/* defined(vax)||defined(tahoe) */
112            xp= x*p     ;
113 	   xh= x*half  ;
114            w = xh-(q-xp)  ;
115 	   p = p+p;
116 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
117 	   return(z*half+c);
118 	}
119 	/* end of |x| > small */
120 
121 	else {
122 	    if(x!=zero) one+small;	/* raise the inexact flag */
123 	    return(copysign(zero,x));
124 	}
125 }
126