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*26893Selefunt "@(#)expm1.c 1.2 (Berkeley) 8/21/85; 1.3 (ucb.elefunt) 03/16/86"; 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 7024595Szliu #ifdef VAX /* VAX D format */ 71*26893Selefunt /* static double */ 7224595Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 7324595Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 7424595Szliu /* lnhuge = 9.4961163736712506989E1 , Hex 2^ 7 * .BDEC1DA73E9010 */ 7524595Szliu /* invln2 = 1.4426950408889634148E0 ; Hex 2^ 1 * .B8AA3B295C17F1 */ 7624595Szliu static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 7724595Szliu static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 7824595Szliu static long lnhugex[] = { 0xec1d43bd, 0x9010a73e}; 7924595Szliu static long invln2x[] = { 0xaa3b40b8, 0x17f1295c}; 8024595Szliu #define ln2hi (*(double*)ln2hix) 8124595Szliu #define ln2lo (*(double*)ln2lox) 8224595Szliu #define lnhuge (*(double*)lnhugex) 8324595Szliu #define invln2 (*(double*)invln2x) 8424595Szliu #else /* IEEE double */ 85*26893Selefunt static double 8624595Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 8724595Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 8824595Szliu lnhuge = 7.1602103751842355450E2 , /*Hex 2^ 9 * 1.6602B15B7ECF2 */ 8924595Szliu invln2 = 1.4426950408889633870E0 ; /*Hex 2^ 0 * 1.71547652B82FE */ 9024595Szliu #endif 9124595Szliu 9224595Szliu double expm1(x) 9324595Szliu double x; 9424595Szliu { 95*26893Selefunt static double one=1.0, half=1.0/2.0; 9624595Szliu double scalb(), copysign(), exp__E(), z,hi,lo,c; 9724595Szliu int k,finite(); 9824595Szliu #ifdef VAX 9924595Szliu static prec=56; 10024595Szliu #else /* IEEE double */ 10124595Szliu static prec=53; 10224595Szliu #endif 10324595Szliu #ifndef VAX 10424595Szliu if(x!=x) return(x); /* x is NaN */ 10524595Szliu #endif 10624595Szliu 10724595Szliu if( x <= lnhuge ) { 10824595Szliu if( x >= -40.0 ) { 10924595Szliu 11024595Szliu /* argument reduction : x - k*ln2 */ 11124595Szliu k= invln2 *x+copysign(0.5,x); /* k=NINT(x/ln2) */ 11224595Szliu hi=x-k*ln2hi ; 11324595Szliu z=hi-(lo=k*ln2lo); 11424595Szliu c=(hi-z)-lo; 11524595Szliu 11624595Szliu if(k==0) return(z+exp__E(z,c)); 11724595Szliu if(k==1) 11824595Szliu if(z< -0.25) 11924595Szliu {x=z+half;x +=exp__E(z,c); return(x+x);} 12024595Szliu else 12124595Szliu {z+=exp__E(z,c); x=half+z; return(x+x);} 12224595Szliu /* end of k=1 */ 12324595Szliu 12424595Szliu else { 12524595Szliu if(k<=prec) 12624595Szliu { x=one-scalb(one,-k); z += exp__E(z,c);} 12724595Szliu else if(k<100) 12824595Szliu { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;} 12924595Szliu else 13024595Szliu { x = exp__E(z,c)+z; z=one;} 13124595Szliu 13224595Szliu return (scalb(x+z,k)); 13324595Szliu } 13424595Szliu } 13524595Szliu /* end of x > lnunfl */ 13624595Szliu 13724595Szliu else 13824595Szliu /* expm1(-big#) rounded to -1 (inexact) */ 13924595Szliu if(finite(x)) 14024595Szliu { ln2hi+ln2lo; return(-one);} 14124595Szliu 14224595Szliu /* expm1(-INF) is -1 */ 14324595Szliu else return(-one); 14424595Szliu } 14524595Szliu /* end of x < lnhuge */ 14624595Szliu 14724595Szliu else 14824595Szliu /* expm1(INF) is INF, expm1(+big#) overflows to INF */ 14924595Szliu return( finite(x) ? scalb(one,5000) : x); 15024595Szliu } 151