124595Szliu /* 224595Szliu * Copyright (c) 1985 Regents of the University of California. 324595Szliu * 424595Szliu * Use and reproduction of this software are granted in accordance with 524595Szliu * the terms and conditions specified in the Berkeley Software License 624595Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724595Szliu * source, and inclusion of this notice) with the additional understanding 824595Szliu * that all recipients should regard themselves as participants in an 924595Szliu * ongoing research project and hence should feel obligated to report 1024595Szliu * their experiences (good or bad) with these elementary function codes, 1124595Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224595Szliu */ 1324595Szliu 1424595Szliu #ifndef lint 1524706Selefunt static char sccsid[] = 16*31812Szliu "@(#)expm1.c 1.2 (Berkeley) 8/21/85; 1.5 (ucb.elefunt) 07/10/87"; 1724595Szliu #endif not lint 1824595Szliu 1924595Szliu /* EXPM1(X) 2024595Szliu * RETURN THE EXPONENTIAL OF X MINUS ONE 2124595Szliu * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS) 2224595Szliu * CODED IN C BY K.C. NG, 1/19/85; 2324595Szliu * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85. 2424595Szliu * 2524595Szliu * Required system supported functions: 2624595Szliu * scalb(x,n) 2724595Szliu * copysign(x,y) 2824595Szliu * finite(x) 2924595Szliu * 3024595Szliu * Kernel function: 3124595Szliu * exp__E(x,c) 3224595Szliu * 3324595Szliu * Method: 3424595Szliu * 1. Argument Reduction: given the input x, find r and integer k such 3524595Szliu * that 3624595Szliu * x = k*ln2 + r, |r| <= 0.5*ln2 . 3724595Szliu * r will be represented as r := z+c for better accuracy. 3824595Szliu * 3924595Szliu * 2. Compute EXPM1(r)=exp(r)-1 by 4024595Szliu * 4124595Szliu * EXPM1(r=z+c) := z + exp__E(z,c) 4224595Szliu * 4324595Szliu * 3. EXPM1(x) = 2^k * ( EXPM1(r) + 1-2^-k ). 4424595Szliu * 4524595Szliu * Remarks: 4624595Szliu * 1. When k=1 and z < -0.25, we use the following formula for 4724595Szliu * better accuracy: 4824595Szliu * EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) ) 4924595Szliu * 2. To avoid rounding error in 1-2^-k where k is large, we use 5024595Szliu * EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 } 5124595Szliu * when k>56. 5224595Szliu * 5324595Szliu * Special cases: 5424595Szliu * EXPM1(INF) is INF, EXPM1(NaN) is NaN; 5524595Szliu * EXPM1(-INF)= -1; 5624595Szliu * for finite argument, only EXPM1(0)=0 is exact. 5724595Szliu * 5824595Szliu * Accuracy: 5924595Szliu * EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with 6024595Szliu * 1,166,000 random arguments on a VAX, the maximum observed error was 6124595Szliu * .872 ulps (units of the last place). 6224595Szliu * 6324595Szliu * Constants: 6424595Szliu * The hexadecimal values are the intended ones for the following constants. 6524595Szliu * The decimal values may be used, provided that the compiler will convert 6624595Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6724595Szliu * shown. 6824595Szliu */ 6924595Szliu 7031790Szliu #if (defined(VAX)||defined(TAHOE)) /* VAX D format */ 71*31812Szliu #ifdef VAX 72*31812Szliu #define _0x(A,B) 0x/**/A/**/B 73*31812Szliu #else /* VAX */ 74*31812Szliu #define _0x(A,B) 0x/**/B/**/A 75*31812Szliu #endif /* VAX */ 7626893Selefunt /* static double */ 7724595Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 7824595Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 7924595Szliu /* lnhuge = 9.4961163736712506989E1 , Hex 2^ 7 * .BDEC1DA73E9010 */ 8024595Szliu /* invln2 = 1.4426950408889634148E0 ; Hex 2^ 1 * .B8AA3B295C17F1 */ 81*31812Szliu static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 82*31812Szliu static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 83*31812Szliu static long lnhugex[] = { _0x(ec1d,43bd), _0x(9010,a73e)}; 84*31812Szliu static long invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)}; 8524595Szliu #define ln2hi (*(double*)ln2hix) 8624595Szliu #define ln2lo (*(double*)ln2lox) 8724595Szliu #define lnhuge (*(double*)lnhugex) 8824595Szliu #define invln2 (*(double*)invln2x) 8924595Szliu #else /* IEEE double */ 9026893Selefunt static double 9124595Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 9224595Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 9324595Szliu lnhuge = 7.1602103751842355450E2 , /*Hex 2^ 9 * 1.6602B15B7ECF2 */ 9424595Szliu invln2 = 1.4426950408889633870E0 ; /*Hex 2^ 0 * 1.71547652B82FE */ 9524595Szliu #endif 9624595Szliu 9724595Szliu double expm1(x) 9824595Szliu double x; 9924595Szliu { 10026893Selefunt static double one=1.0, half=1.0/2.0; 10124595Szliu double scalb(), copysign(), exp__E(), z,hi,lo,c; 10224595Szliu int k,finite(); 10331790Szliu #if (defined(VAX)||defined(TAHOE)) 10424595Szliu static prec=56; 10524595Szliu #else /* IEEE double */ 10624595Szliu static prec=53; 10724595Szliu #endif 10831790Szliu #if (!defined(VAX)&&!defined(TAHOE)) 10924595Szliu if(x!=x) return(x); /* x is NaN */ 11024595Szliu #endif 11124595Szliu 11224595Szliu if( x <= lnhuge ) { 11324595Szliu if( x >= -40.0 ) { 11424595Szliu 11524595Szliu /* argument reduction : x - k*ln2 */ 11624595Szliu k= invln2 *x+copysign(0.5,x); /* k=NINT(x/ln2) */ 11724595Szliu hi=x-k*ln2hi ; 11824595Szliu z=hi-(lo=k*ln2lo); 11924595Szliu c=(hi-z)-lo; 12024595Szliu 12124595Szliu if(k==0) return(z+exp__E(z,c)); 12224595Szliu if(k==1) 12324595Szliu if(z< -0.25) 12424595Szliu {x=z+half;x +=exp__E(z,c); return(x+x);} 12524595Szliu else 12624595Szliu {z+=exp__E(z,c); x=half+z; return(x+x);} 12724595Szliu /* end of k=1 */ 12824595Szliu 12924595Szliu else { 13024595Szliu if(k<=prec) 13124595Szliu { x=one-scalb(one,-k); z += exp__E(z,c);} 13224595Szliu else if(k<100) 13324595Szliu { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;} 13424595Szliu else 13524595Szliu { x = exp__E(z,c)+z; z=one;} 13624595Szliu 13724595Szliu return (scalb(x+z,k)); 13824595Szliu } 13924595Szliu } 14024595Szliu /* end of x > lnunfl */ 14124595Szliu 14224595Szliu else 14324595Szliu /* expm1(-big#) rounded to -1 (inexact) */ 14424595Szliu if(finite(x)) 14524595Szliu { ln2hi+ln2lo; return(-one);} 14624595Szliu 14724595Szliu /* expm1(-INF) is -1 */ 14824595Szliu else return(-one); 14924595Szliu } 15024595Szliu /* end of x < lnhuge */ 15124595Szliu 15224595Szliu else 15324595Szliu /* expm1(INF) is INF, expm1(+big#) overflows to INF */ 15424595Szliu return( finite(x) ? scalb(one,5000) : x); 15524595Szliu } 156