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