1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * 4 * Use and reproduction of this software are granted in accordance with 5 * the terms and conditions specified in the Berkeley Software License 6 * Agreement (in particular, this entails acknowledgement of the programs' 7 * source, and inclusion of this notice) with the additional understanding 8 * that all recipients should regard themselves as participants in an 9 * ongoing research project and hence should feel obligated to report 10 * their experiences (good or bad) with these elementary function codes, 11 * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 12 */ 13 14 #ifndef lint 15 static char sccsid[] = 16 "@(#)exp__E.c 1.2 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87"; 17 #endif /* not lint */ 18 19 /* exp__E(x,c) 20 * ASSUMPTION: c << x SO THAT fl(x+c)=x. 21 * (c is the correction term for x) 22 * exp__E RETURNS 23 * 24 * / exp(x+c) - 1 - x , 1E-19 < |x| < .3465736 25 * exp__E(x,c) = | 26 * \ 0 , |x| < 1E-19. 27 * 28 * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS) 29 * KERNEL FUNCTION OF EXP, EXPM1, POW FUNCTIONS 30 * CODED IN C BY K.C. NG, 1/31/85; 31 * REVISED BY K.C. NG on 3/16/85, 4/16/85. 32 * 33 * Required system supported function: 34 * copysign(x,y) 35 * 36 * Method: 37 * 1. Rational approximation. Let r=x+c. 38 * Based on 39 * 2 * sinh(r/2) 40 * exp(r) - 1 = ---------------------- , 41 * cosh(r/2) - sinh(r/2) 42 * exp__E(r) is computed using 43 * x*x (x/2)*W - ( Q - ( 2*P + x*P ) ) 44 * --- + (c + x*[---------------------------------- + c ]) 45 * 2 1 - W 46 * where P := p1*x^2 + p2*x^4, 47 * Q := q1*x^2 + q2*x^4 (for 56 bits precision, add q3*x^6) 48 * W := x/2-(Q-x*P), 49 * 50 * (See the listing below for the values of p1,p2,q1,q2,q3. The poly- 51 * nomials P and Q may be regarded as the approximations to sinh 52 * and cosh : 53 * sinh(r/2) = r/2 + r * P , cosh(r/2) = 1 + Q . ) 54 * 55 * The coefficients were obtained by a special Remez algorithm. 56 * 57 * Approximation error: 58 * 59 * | exp(x) - 1 | 2**(-57), (IEEE double) 60 * | ------------ - (exp__E(x,0)+x)/x | <= 61 * | x | 2**(-69). (VAX D) 62 * 63 * Constants: 64 * The hexadecimal values are the intended ones for the following constants. 65 * The decimal values may be used, provided that the compiler will convert 66 * from decimal to binary accurately enough to produce the hexadecimal values 67 * shown. 68 */ 69 70 #if defined(vax)||defined(tahoe) /* VAX D format */ 71 #ifdef vax 72 #define _0x(A,B) 0x/**/A/**/B 73 #else /* vax */ 74 #define _0x(A,B) 0x/**/B/**/A 75 #endif /* vax */ 76 /* static double */ 77 /* p1 = 1.5150724356786683059E-2 , Hex 2^ -6 * .F83ABE67E1066A */ 78 /* p2 = 6.3112487873718332688E-5 , Hex 2^-13 * .845B4248CD0173 */ 79 /* q1 = 1.1363478204690669916E-1 , Hex 2^ -3 * .E8B95A44A2EC45 */ 80 /* q2 = 1.2624568129896839182E-3 , Hex 2^ -9 * .A5790572E4F5E7 */ 81 /* q3 = 1.5021856115869022674E-6 ; Hex 2^-19 * .C99EB4604AC395 */ 82 static long p1x[] = { _0x(3abe,3d78), _0x(066a,67e1)}; 83 static long p2x[] = { _0x(5b42,3984), _0x(0173,48cd)}; 84 static long q1x[] = { _0x(b95a,3ee8), _0x(ec45,44a2)}; 85 static long q2x[] = { _0x(7905,3ba5), _0x(f5e7,72e4)}; 86 static long q3x[] = { _0x(9eb4,36c9), _0x(c395,604a)}; 87 #define p1 (*(double*)p1x) 88 #define p2 (*(double*)p2x) 89 #define q1 (*(double*)q1x) 90 #define q2 (*(double*)q2x) 91 #define q3 (*(double*)q3x) 92 #else /* defined(vax)||defined(tahoe) */ 93 static double 94 p1 = 1.3887401997267371720E-2 , /*Hex 2^ -7 * 1.C70FF8B3CC2CF */ 95 p2 = 3.3044019718331897649E-5 , /*Hex 2^-15 * 1.15317DF4526C4 */ 96 q1 = 1.1110813732786649355E-1 , /*Hex 2^ -4 * 1.C719538248597 */ 97 q2 = 9.9176615021572857300E-4 ; /*Hex 2^-10 * 1.03FC4CB8C98E8 */ 98 #endif /* defined(vax)||defined(tahoe) */ 99 100 double exp__E(x,c) 101 double x,c; 102 { 103 static double zero=0.0, one=1.0, half=1.0/2.0, small=1.0E-19; 104 double copysign(),z,p,q,xp,xh,w; 105 if(copysign(x,one)>small) { 106 z = x*x ; 107 p = z*( p1 +z* p2 ); 108 #if defined(vax)||defined(tahoe) 109 q = z*( q1 +z*( q2 +z* q3 )); 110 #else /* defined(vax)||defined(tahoe) */ 111 q = z*( q1 +z* q2 ); 112 #endif /* defined(vax)||defined(tahoe) */ 113 xp= x*p ; 114 xh= x*half ; 115 w = xh-(q-xp) ; 116 p = p+p; 117 c += x*((xh*w-(q-(p+xp)))/(one-w)+c); 118 return(z*half+c); 119 } 120 /* end of |x| > small */ 121 122 else { 123 if(x!=zero) one+small; /* raise the inexact flag */ 124 return(copysign(zero,x)); 125 } 126 } 127