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 "@(#)expm1.c 1.2 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87"; 17 #endif /* not lint */ 18 19 /* EXPM1(X) 20 * RETURN THE EXPONENTIAL OF X MINUS ONE 21 * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS) 22 * CODED IN C BY K.C. NG, 1/19/85; 23 * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85. 24 * 25 * Required system supported functions: 26 * scalb(x,n) 27 * copysign(x,y) 28 * finite(x) 29 * 30 * Kernel function: 31 * exp__E(x,c) 32 * 33 * Method: 34 * 1. Argument Reduction: given the input x, find r and integer k such 35 * that 36 * x = k*ln2 + r, |r| <= 0.5*ln2 . 37 * r will be represented as r := z+c for better accuracy. 38 * 39 * 2. Compute EXPM1(r)=exp(r)-1 by 40 * 41 * EXPM1(r=z+c) := z + exp__E(z,c) 42 * 43 * 3. EXPM1(x) = 2^k * ( EXPM1(r) + 1-2^-k ). 44 * 45 * Remarks: 46 * 1. When k=1 and z < -0.25, we use the following formula for 47 * better accuracy: 48 * EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) ) 49 * 2. To avoid rounding error in 1-2^-k where k is large, we use 50 * EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 } 51 * when k>56. 52 * 53 * Special cases: 54 * EXPM1(INF) is INF, EXPM1(NaN) is NaN; 55 * EXPM1(-INF)= -1; 56 * for finite argument, only EXPM1(0)=0 is exact. 57 * 58 * Accuracy: 59 * EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with 60 * 1,166,000 random arguments on a VAX, the maximum observed error was 61 * .872 ulps (units of the last place). 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 /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 78 /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 79 /* lnhuge = 9.4961163736712506989E1 , Hex 2^ 7 * .BDEC1DA73E9010 */ 80 /* invln2 = 1.4426950408889634148E0 ; Hex 2^ 1 * .B8AA3B295C17F1 */ 81 static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 82 static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 83 static long lnhugex[] = { _0x(ec1d,43bd), _0x(9010,a73e)}; 84 static long invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)}; 85 #define ln2hi (*(double*)ln2hix) 86 #define ln2lo (*(double*)ln2lox) 87 #define lnhuge (*(double*)lnhugex) 88 #define invln2 (*(double*)invln2x) 89 #else /* defined(vax)||defined(tahoe) */ 90 static double 91 ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 92 ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 93 lnhuge = 7.1602103751842355450E2 , /*Hex 2^ 9 * 1.6602B15B7ECF2 */ 94 invln2 = 1.4426950408889633870E0 ; /*Hex 2^ 0 * 1.71547652B82FE */ 95 #endif /* defined(vax)||defined(tahoe) */ 96 97 double expm1(x) 98 double x; 99 { 100 static double one=1.0, half=1.0/2.0; 101 double scalb(), copysign(), exp__E(), z,hi,lo,c; 102 int k,finite(); 103 #if defined(vax)||defined(tahoe) 104 static prec=56; 105 #else /* defined(vax)||defined(tahoe) */ 106 static prec=53; 107 #endif /* defined(vax)||defined(tahoe) */ 108 #if !defined(vax)&&!defined(tahoe) 109 if(x!=x) return(x); /* x is NaN */ 110 #endif /* !defined(vax)&&!defined(tahoe) */ 111 112 if( x <= lnhuge ) { 113 if( x >= -40.0 ) { 114 115 /* argument reduction : x - k*ln2 */ 116 k= invln2 *x+copysign(0.5,x); /* k=NINT(x/ln2) */ 117 hi=x-k*ln2hi ; 118 z=hi-(lo=k*ln2lo); 119 c=(hi-z)-lo; 120 121 if(k==0) return(z+exp__E(z,c)); 122 if(k==1) 123 if(z< -0.25) 124 {x=z+half;x +=exp__E(z,c); return(x+x);} 125 else 126 {z+=exp__E(z,c); x=half+z; return(x+x);} 127 /* end of k=1 */ 128 129 else { 130 if(k<=prec) 131 { x=one-scalb(one,-k); z += exp__E(z,c);} 132 else if(k<100) 133 { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;} 134 else 135 { x = exp__E(z,c)+z; z=one;} 136 137 return (scalb(x+z,k)); 138 } 139 } 140 /* end of x > lnunfl */ 141 142 else 143 /* expm1(-big#) rounded to -1 (inexact) */ 144 if(finite(x)) 145 { ln2hi+ln2lo; return(-one);} 146 147 /* expm1(-INF) is -1 */ 148 else return(-one); 149 } 150 /* end of x < lnhuge */ 151 152 else 153 /* expm1(INF) is INF, expm1(+big#) overflows to INF */ 154 return( finite(x) ? scalb(one,5000) : x); 155 } 156