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