134126Sbostic /*
224594Szliu  * Copyright (c) 1985 Regents of the University of California.
334126Sbostic  * All rights reserved.
434126Sbostic  *
534126Sbostic  * 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.
1634126Sbostic  *
1734126Sbostic  * All recipients should regard themselves as participants in an ongoing
1834126Sbostic  * research project and hence should feel obligated to report their
1934126Sbostic  * experiences (good or bad) with these elementary function codes, using
2034126Sbostic  * the sendbug(8) program, to the authors.
2124594Szliu  */
2224594Szliu 
2324594Szliu #ifndef lint
24*35679Sbostic static char sccsid[] = "@(#)exp__E.c	5.4 (Berkeley) 09/22/88";
2534126Sbostic #endif /* not lint */
2624594Szliu 
2724594Szliu /* exp__E(x,c)
2824594Szliu  * ASSUMPTION: c << x  SO THAT  fl(x+c)=x.
2924594Szliu  * (c is the correction term for x)
3024594Szliu  * exp__E RETURNS
3124594Szliu  *
3224594Szliu  *			 /  exp(x+c) - 1 - x ,  1E-19 < |x| < .3465736
3324594Szliu  *       exp__E(x,c) = 	|
3424594Szliu  *			 \  0 ,  |x| < 1E-19.
3524594Szliu  *
3624594Szliu  * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS)
3724594Szliu  * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS
3824594Szliu  * CODED IN C BY K.C. NG, 1/31/85;
3924594Szliu  * REVISED BY K.C. NG on 3/16/85, 4/16/85.
4024594Szliu  *
4124594Szliu  * Required system supported function:
4224594Szliu  *	copysign(x,y)
4324594Szliu  *
4424594Szliu  * Method:
4524594Szliu  *	1. Rational approximation. Let r=x+c.
4624594Szliu  *	   Based on
4724594Szliu  *                                   2 * sinh(r/2)
4824594Szliu  *                exp(r) - 1 =   ----------------------   ,
4924594Szliu  *                               cosh(r/2) - sinh(r/2)
5024594Szliu  *	   exp__E(r) is computed using
5124594Szliu  *                   x*x            (x/2)*W - ( Q - ( 2*P  + x*P ) )
5224594Szliu  *                   --- + (c + x*[---------------------------------- + c ])
5324594Szliu  *                    2                          1 - W
5424594Szliu  * 	   where  P := p1*x^2 + p2*x^4,
5524594Szliu  *	          Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6)
5624594Szliu  *	          W := x/2-(Q-x*P),
5724594Szliu  *
5824594Szliu  *	   (See the listing below for the values of p1,p2,q1,q2,q3. The poly-
5924594Szliu  *	    nomials P and Q may be regarded as the approximations to sinh
6024594Szliu  *	    and cosh :
6124594Szliu  *		sinh(r/2) =  r/2 + r * P  ,  cosh(r/2) =  1 + Q . )
6224594Szliu  *
6324594Szliu  *         The coefficients were obtained by a special Remez algorithm.
6424594Szliu  *
6524594Szliu  * Approximation error:
6624594Szliu  *
6724594Szliu  *   |	exp(x) - 1			   |        2**(-57),  (IEEE double)
6824594Szliu  *   | ------------  -  (exp__E(x,0)+x)/x  |  <=
6924594Szliu  *   |	     x			           |	    2**(-69).  (VAX D)
7024594Szliu  *
7124594Szliu  * Constants:
7224594Szliu  * The hexadecimal values are the intended ones for the following constants.
7324594Szliu  * The decimal values may be used, provided that the compiler will convert
7424594Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
7524594Szliu  * shown.
7624594Szliu  */
7724594Szliu 
78*35679Sbostic #include "mathimpl.h"
7924594Szliu 
80*35679Sbostic vc(p1, 1.5150724356786683059E-2 ,3abe,3d78,066a,67e1,  -6, .F83ABE67E1066A)
81*35679Sbostic vc(p2, 6.3112487873718332688E-5 ,5b42,3984,0173,48cd, -13, .845B4248CD0173)
82*35679Sbostic vc(q1, 1.1363478204690669916E-1 ,b95a,3ee8,ec45,44a2,  -3, .E8B95A44A2EC45)
83*35679Sbostic vc(q2, 1.2624568129896839182E-3 ,7905,3ba5,f5e7,72e4,  -9, .A5790572E4F5E7)
84*35679Sbostic vc(q3, 1.5021856115869022674E-6 ,9eb4,36c9,c395,604a, -19, .C99EB4604AC395)
85*35679Sbostic 
86*35679Sbostic ic(p1, 1.3887401997267371720E-2,  -7, 1.C70FF8B3CC2CF)
87*35679Sbostic ic(p2, 3.3044019718331897649E-5, -15, 1.15317DF4526C4)
88*35679Sbostic ic(q1, 1.1110813732786649355E-1,  -4, 1.C719538248597)
89*35679Sbostic ic(q2, 9.9176615021572857300E-4, -10, 1.03FC4CB8C98E8)
90*35679Sbostic 
91*35679Sbostic #ifdef vccast
92*35679Sbostic #define       p1    vccast(p1)
93*35679Sbostic #define       p2    vccast(p2)
94*35679Sbostic #define       q1    vccast(q1)
95*35679Sbostic #define       q2    vccast(q2)
96*35679Sbostic #define       q3    vccast(q3)
97*35679Sbostic #endif
98*35679Sbostic 
9924594Szliu double exp__E(x,c)
10024594Szliu double x,c;
10124594Szliu {
102*35679Sbostic 	const static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19;
103*35679Sbostic 	double z,p,q,xp,xh,w;
10424594Szliu 	if(copysign(x,one)>small) {
10524594Szliu            z = x*x  ;
10624594Szliu 	   p = z*( p1 +z* p2 );
10731853Szliu #if defined(vax)||defined(tahoe)
10824594Szliu            q = z*( q1 +z*( q2 +z* q3 ));
10931853Szliu #else	/* defined(vax)||defined(tahoe) */
11024594Szliu            q = z*( q1 +z*  q2 );
11131853Szliu #endif	/* defined(vax)||defined(tahoe) */
11224594Szliu            xp= x*p     ;
11324594Szliu 	   xh= x*half  ;
11424594Szliu            w = xh-(q-xp)  ;
11524594Szliu 	   p = p+p;
11624594Szliu 	   c += x*((xh*w-(q-(p+xp)))/(one-w)+c);
11724594Szliu 	   return(z*half+c);
11824594Szliu 	}
11924594Szliu 	/* end of |x| > small */
12024594Szliu 
12124594Szliu 	else {
12224594Szliu 	    if(x!=zero) one+small;	/* raise the inexact flag */
12324594Szliu 	    return(copysign(zero,x));
12424594Szliu 	}
12524594Szliu }
126