1*24593Szliu /* 2*24593Szliu * Copyright (c) 1985 Regents of the University of California. 3*24593Szliu * 4*24593Szliu * Use and reproduction of this software are granted in accordance with 5*24593Szliu * the terms and conditions specified in the Berkeley Software License 6*24593Szliu * Agreement (in particular, this entails acknowledgement of the programs' 7*24593Szliu * source, and inclusion of this notice) with the additional understanding 8*24593Szliu * that all recipients should regard themselves as participants in an 9*24593Szliu * ongoing research project and hence should feel obligated to report 10*24593Szliu * their experiences (good or bad) with these elementary function codes, 11*24593Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 12*24593Szliu */ 13*24593Szliu 14*24593Szliu #ifndef lint 15*24593Szliu static char sccsid[] = "@(#)exp.c 1.1 (ELEFUNT) 09/06/85"; 16*24593Szliu #endif not lint 17*24593Szliu 18*24593Szliu /* EXP(X) 19*24593Szliu * RETURN THE EXPONENTIAL OF X 20*24593Szliu * DOUBLE PRECISION (IEEE 53 bits, VAX D FORMAT 56 BITS) 21*24593Szliu * CODED IN C BY K.C. NG, 1/19/85; 22*24593Szliu * REVISED BY K.C. NG on 2/6/85, 2/15/85, 3/7/85, 3/24/85, 4/16/85. 23*24593Szliu * 24*24593Szliu * Required system supported functions: 25*24593Szliu * scalb(x,n) 26*24593Szliu * copysign(x,y) 27*24593Szliu * finite(x) 28*24593Szliu * 29*24593Szliu * Kernel function: 30*24593Szliu * exp__E(x,c) 31*24593Szliu * 32*24593Szliu * Method: 33*24593Szliu * 1. Argument Reduction: given the input x, find r and integer k such 34*24593Szliu * that 35*24593Szliu * x = k*ln2 + r, |r| <= 0.5*ln2 . 36*24593Szliu * r will be represented as r := z+c for better accuracy. 37*24593Szliu * 38*24593Szliu * 2. Compute expm1(r)=exp(r)-1 by 39*24593Szliu * 40*24593Szliu * expm1(r=z+c) := z + exp__E(z,r) 41*24593Szliu * 42*24593Szliu * 3. exp(x) = 2^k * ( expm1(r) + 1 ). 43*24593Szliu * 44*24593Szliu * Special cases: 45*24593Szliu * exp(INF) is INF, exp(NaN) is NaN; 46*24593Szliu * exp(-INF)= 0; 47*24593Szliu * for finite argument, only exp(0)=1 is exact. 48*24593Szliu * 49*24593Szliu * Accuracy: 50*24593Szliu * exp(x) returns the exponential of x nearly rounded. In a test run 51*24593Szliu * with 1,156,000 random arguments on a VAX, the maximum observed 52*24593Szliu * error was .768 ulps (units in the last place). 53*24593Szliu * 54*24593Szliu * Constants: 55*24593Szliu * The hexadecimal values are the intended ones for the following constants. 56*24593Szliu * The decimal values may be used, provided that the compiler will convert 57*24593Szliu * from decimal to binary accurately enough to produce the hexadecimal values 58*24593Szliu * shown. 59*24593Szliu */ 60*24593Szliu 61*24593Szliu #ifdef VAX /* VAX D format */ 62*24593Szliu /* double static */ 63*24593Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 64*24593Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 65*24593Szliu /* lnhuge = 9.4961163736712506989E1 , Hex 2^ 7 * .BDEC1DA73E9010 */ 66*24593Szliu /* lntiny = -9.5654310917272452386E1 , Hex 2^ 7 * -.BF4F01D72E33AF */ 67*24593Szliu /* invln2 = 1.4426950408889634148E0 ; Hex 2^ 1 * .B8AA3B295C17F1 */ 68*24593Szliu static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 69*24593Szliu static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 70*24593Szliu static long lnhugex[] = { 0xec1d43bd, 0x9010a73e}; 71*24593Szliu static long lntinyx[] = { 0x4f01c3bf, 0x33afd72e}; 72*24593Szliu static long invln2x[] = { 0xaa3b40b8, 0x17f1295c}; 73*24593Szliu #define ln2hi (*(double*)ln2hix) 74*24593Szliu #define ln2lo (*(double*)ln2lox) 75*24593Szliu #define lnhuge (*(double*)lnhugex) 76*24593Szliu #define lntiny (*(double*)lntinyx) 77*24593Szliu #define invln2 (*(double*)invln2x) 78*24593Szliu #else /* IEEE double */ 79*24593Szliu double static 80*24593Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 81*24593Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 82*24593Szliu lnhuge = 7.1602103751842355450E2 , /*Hex 2^ 9 * 1.6602B15B7ECF2 */ 83*24593Szliu lntiny = -7.5137154372698068983E2 , /*Hex 2^ 9 * -1.77AF8EBEAE354 */ 84*24593Szliu invln2 = 1.4426950408889633870E0 ; /*Hex 2^ 0 * 1.71547652B82FE */ 85*24593Szliu #endif 86*24593Szliu 87*24593Szliu double exp(x) 88*24593Szliu double x; 89*24593Szliu { 90*24593Szliu double scalb(), copysign(), exp__E(), z,hi,lo,c; 91*24593Szliu int k,finite(); 92*24593Szliu 93*24593Szliu #ifndef VAX 94*24593Szliu if(x!=x) return(x); /* x is NaN */ 95*24593Szliu #endif 96*24593Szliu if( x <= lnhuge ) { 97*24593Szliu if( x >= lntiny ) { 98*24593Szliu 99*24593Szliu /* argument reduction : x --> x - k*ln2 */ 100*24593Szliu 101*24593Szliu k=invln2*x+copysign(0.5,x); /* k=NINT(x/ln2) */ 102*24593Szliu 103*24593Szliu /* express x-k*ln2 as z+c */ 104*24593Szliu hi=x-k*ln2hi; 105*24593Szliu z=hi-(lo=k*ln2lo); 106*24593Szliu c=(hi-z)-lo; 107*24593Szliu 108*24593Szliu /* return 2^k*[expm1(x) + 1] */ 109*24593Szliu z += exp__E(z,c); 110*24593Szliu return (scalb(z+1.0,k)); 111*24593Szliu } 112*24593Szliu /* end of x > lntiny */ 113*24593Szliu 114*24593Szliu else 115*24593Szliu /* exp(-big#) underflows to zero */ 116*24593Szliu if(finite(x)) return(scalb(1.0,-5000)); 117*24593Szliu 118*24593Szliu /* exp(-INF) is zero */ 119*24593Szliu else return(0.0); 120*24593Szliu } 121*24593Szliu /* end of x < lnhuge */ 122*24593Szliu 123*24593Szliu else 124*24593Szliu /* exp(INF) is INF, exp(+big#) overflows to INF */ 125*24593Szliu return( finite(x) ? scalb(1.0,5000) : x); 126*24593Szliu } 127