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.3 (ucb.elefunt) 03/16/86"; 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 #ifdef VAX /* VAX D format */ 71 /* static double */ 72 /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 73 /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 74 /* lnhuge = 9.4961163736712506989E1 , Hex 2^ 7 * .BDEC1DA73E9010 */ 75 /* invln2 = 1.4426950408889634148E0 ; Hex 2^ 1 * .B8AA3B295C17F1 */ 76 static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 77 static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 78 static long lnhugex[] = { 0xec1d43bd, 0x9010a73e}; 79 static long invln2x[] = { 0xaa3b40b8, 0x17f1295c}; 80 #define ln2hi (*(double*)ln2hix) 81 #define ln2lo (*(double*)ln2lox) 82 #define lnhuge (*(double*)lnhugex) 83 #define invln2 (*(double*)invln2x) 84 #else /* IEEE double */ 85 static double 86 ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 87 ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 88 lnhuge = 7.1602103751842355450E2 , /*Hex 2^ 9 * 1.6602B15B7ECF2 */ 89 invln2 = 1.4426950408889633870E0 ; /*Hex 2^ 0 * 1.71547652B82FE */ 90 #endif 91 92 double expm1(x) 93 double x; 94 { 95 static double one=1.0, half=1.0/2.0; 96 double scalb(), copysign(), exp__E(), z,hi,lo,c; 97 int k,finite(); 98 #ifdef VAX 99 static prec=56; 100 #else /* IEEE double */ 101 static prec=53; 102 #endif 103 #ifndef VAX 104 if(x!=x) return(x); /* x is NaN */ 105 #endif 106 107 if( x <= lnhuge ) { 108 if( x >= -40.0 ) { 109 110 /* argument reduction : x - k*ln2 */ 111 k= invln2 *x+copysign(0.5,x); /* k=NINT(x/ln2) */ 112 hi=x-k*ln2hi ; 113 z=hi-(lo=k*ln2lo); 114 c=(hi-z)-lo; 115 116 if(k==0) return(z+exp__E(z,c)); 117 if(k==1) 118 if(z< -0.25) 119 {x=z+half;x +=exp__E(z,c); return(x+x);} 120 else 121 {z+=exp__E(z,c); x=half+z; return(x+x);} 122 /* end of k=1 */ 123 124 else { 125 if(k<=prec) 126 { x=one-scalb(one,-k); z += exp__E(z,c);} 127 else if(k<100) 128 { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;} 129 else 130 { x = exp__E(z,c)+z; z=one;} 131 132 return (scalb(x+z,k)); 133 } 134 } 135 /* end of x > lnunfl */ 136 137 else 138 /* expm1(-big#) rounded to -1 (inexact) */ 139 if(finite(x)) 140 { ln2hi+ln2lo; return(-one);} 141 142 /* expm1(-INF) is -1 */ 143 else return(-one); 144 } 145 /* end of x < lnhuge */ 146 147 else 148 /* expm1(INF) is INF, expm1(+big#) overflows to INF */ 149 return( finite(x) ? scalb(one,5000) : x); 150 } 151