1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * All recipients should regard themselves as participants in an ongoing 8 * research project and hence should feel obligated to report their 9 * experiences (good or bad) with these elementary function codes, using 10 * the sendbug(8) program, to the authors. 11 */ 12 13 #ifndef lint 14 static char sccsid[] = "@(#)expm1.c 5.5 (Berkeley) 06/01/90"; 15 #endif /* not lint */ 16 17 /* EXPM1(X) 18 * RETURN THE EXPONENTIAL OF X MINUS ONE 19 * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS) 20 * CODED IN C BY K.C. NG, 1/19/85; 21 * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85. 22 * 23 * Required system supported functions: 24 * scalb(x,n) 25 * copysign(x,y) 26 * finite(x) 27 * 28 * Kernel function: 29 * exp__E(x,c) 30 * 31 * Method: 32 * 1. Argument Reduction: given the input x, find r and integer k such 33 * that 34 * x = k*ln2 + r, |r| <= 0.5*ln2 . 35 * r will be represented as r := z+c for better accuracy. 36 * 37 * 2. Compute EXPM1(r)=exp(r)-1 by 38 * 39 * EXPM1(r=z+c) := z + exp__E(z,c) 40 * 41 * 3. EXPM1(x) = 2^k * ( EXPM1(r) + 1-2^-k ). 42 * 43 * Remarks: 44 * 1. When k=1 and z < -0.25, we use the following formula for 45 * better accuracy: 46 * EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) ) 47 * 2. To avoid rounding error in 1-2^-k where k is large, we use 48 * EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 } 49 * when k>56. 50 * 51 * Special cases: 52 * EXPM1(INF) is INF, EXPM1(NaN) is NaN; 53 * EXPM1(-INF)= -1; 54 * for finite argument, only EXPM1(0)=0 is exact. 55 * 56 * Accuracy: 57 * EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with 58 * 1,166,000 random arguments on a VAX, the maximum observed error was 59 * .872 ulps (units of the last place). 60 * 61 * Constants: 62 * The hexadecimal values are the intended ones for the following constants. 63 * The decimal values may be used, provided that the compiler will convert 64 * from decimal to binary accurately enough to produce the hexadecimal values 65 * shown. 66 */ 67 68 #include "mathimpl.h" 69 70 vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 71 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 72 vc(lnhuge, 9.4961163736712506989E1 ,ec1d,43bd,9010,a73e, 7, .BDEC1DA73E9010) 73 vc(invln2, 1.4426950408889634148E0 ,aa3b,40b8,17f1,295c, 1, .B8AA3B295C17F1) 74 75 ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 76 ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 77 ic(lnhuge, 7.1602103751842355450E2, 9, 1.6602B15B7ECF2) 78 ic(invln2, 1.4426950408889633870E0, 0, 1.71547652B82FE) 79 80 #ifdef vccast 81 #define ln2hi vccast(ln2hi) 82 #define ln2lo vccast(ln2lo) 83 #define lnhuge vccast(lnhuge) 84 #define invln2 vccast(invln2) 85 #endif 86 87 double expm1(x) 88 double x; 89 { 90 const static double one=1.0, half=1.0/2.0; 91 double z,hi,lo,c; 92 int k; 93 #if defined(vax)||defined(tahoe) 94 static prec=56; 95 #else /* defined(vax)||defined(tahoe) */ 96 static prec=53; 97 #endif /* defined(vax)||defined(tahoe) */ 98 99 #if !defined(vax)&&!defined(tahoe) 100 if(x!=x) return(x); /* x is NaN */ 101 #endif /* !defined(vax)&&!defined(tahoe) */ 102 103 if( x <= lnhuge ) { 104 if( x >= -40.0 ) { 105 106 /* argument reduction : x - k*ln2 */ 107 k= invln2 *x+copysign(0.5,x); /* k=NINT(x/ln2) */ 108 hi=x-k*ln2hi ; 109 z=hi-(lo=k*ln2lo); 110 c=(hi-z)-lo; 111 112 if(k==0) return(z+exp__E(z,c)); 113 if(k==1) 114 if(z< -0.25) 115 {x=z+half;x +=exp__E(z,c); return(x+x);} 116 else 117 {z+=exp__E(z,c); x=half+z; return(x+x);} 118 /* end of k=1 */ 119 120 else { 121 if(k<=prec) 122 { x=one-scalb(one,-k); z += exp__E(z,c);} 123 else if(k<100) 124 { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;} 125 else 126 { x = exp__E(z,c)+z; z=one;} 127 128 return (scalb(x+z,k)); 129 } 130 } 131 /* end of x > lnunfl */ 132 133 else 134 /* expm1(-big#) rounded to -1 (inexact) */ 135 if(finite(x)) 136 { ln2hi+ln2lo; return(-one);} 137 138 /* expm1(-INF) is -1 */ 139 else return(-one); 140 } 141 /* end of x < lnhuge */ 142 143 else 144 /* expm1(INF) is INF, expm1(+big#) overflows to INF */ 145 return( finite(x) ? scalb(one,5000) : x); 146 } 147