134124Sbostic /* 224595Szliu * Copyright (c) 1985 Regents of the University of California. 334124Sbostic * All rights reserved. 434124Sbostic * 534124Sbostic * Redistribution and use in source and binary forms are permitted 6*34931Sbostic * provided that the above copyright notice and this paragraph are 7*34931Sbostic * duplicated in all such forms and that any documentation, 8*34931Sbostic * advertising materials, and other materials related to such 9*34931Sbostic * distribution and use acknowledge that the software was developed 10*34931Sbostic * by the University of California, Berkeley. The name of the 11*34931Sbostic * University may not be used to endorse or promote products derived 12*34931Sbostic * from this software without specific prior written permission. 13*34931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34931Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634124Sbostic * 1734124Sbostic * All recipients should regard themselves as participants in an ongoing 1834124Sbostic * research project and hence should feel obligated to report their 1934124Sbostic * experiences (good or bad) with these elementary function codes, using 2034124Sbostic * the sendbug(8) program, to the authors. 2124595Szliu */ 2224595Szliu 2324595Szliu #ifndef lint 24*34931Sbostic static char sccsid[] = "@(#)expm1.c 5.3 (Berkeley) 06/30/88"; 2534124Sbostic #endif /* not lint */ 2624595Szliu 2724595Szliu /* EXPM1(X) 2824595Szliu * RETURN THE EXPONENTIAL OF X MINUS ONE 2924595Szliu * DOUBLE PRECISION (IEEE 53 BITS, VAX D FORMAT 56 BITS) 3024595Szliu * CODED IN C BY K.C. NG, 1/19/85; 3124595Szliu * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/21/85, 4/16/85. 3224595Szliu * 3324595Szliu * Required system supported functions: 3424595Szliu * scalb(x,n) 3524595Szliu * copysign(x,y) 3624595Szliu * finite(x) 3724595Szliu * 3824595Szliu * Kernel function: 3924595Szliu * exp__E(x,c) 4024595Szliu * 4124595Szliu * Method: 4224595Szliu * 1. Argument Reduction: given the input x, find r and integer k such 4324595Szliu * that 4424595Szliu * x = k*ln2 + r, |r| <= 0.5*ln2 . 4524595Szliu * r will be represented as r := z+c for better accuracy. 4624595Szliu * 4724595Szliu * 2. Compute EXPM1(r)=exp(r)-1 by 4824595Szliu * 4924595Szliu * EXPM1(r=z+c) := z + exp__E(z,c) 5024595Szliu * 5124595Szliu * 3. EXPM1(x) = 2^k * ( EXPM1(r) + 1-2^-k ). 5224595Szliu * 5324595Szliu * Remarks: 5424595Szliu * 1. When k=1 and z < -0.25, we use the following formula for 5524595Szliu * better accuracy: 5624595Szliu * EXPM1(x) = 2 * ( (z+0.5) + exp__E(z,c) ) 5724595Szliu * 2. To avoid rounding error in 1-2^-k where k is large, we use 5824595Szliu * EXPM1(x) = 2^k * { [z+(exp__E(z,c)-2^-k )] + 1 } 5924595Szliu * when k>56. 6024595Szliu * 6124595Szliu * Special cases: 6224595Szliu * EXPM1(INF) is INF, EXPM1(NaN) is NaN; 6324595Szliu * EXPM1(-INF)= -1; 6424595Szliu * for finite argument, only EXPM1(0)=0 is exact. 6524595Szliu * 6624595Szliu * Accuracy: 6724595Szliu * EXPM1(x) returns the exact (exp(x)-1) nearly rounded. In a test run with 6824595Szliu * 1,166,000 random arguments on a VAX, the maximum observed error was 6924595Szliu * .872 ulps (units of the last place). 7024595Szliu * 7124595Szliu * Constants: 7224595Szliu * The hexadecimal values are the intended ones for the following constants. 7324595Szliu * The decimal values may be used, provided that the compiler will convert 7424595Szliu * from decimal to binary accurately enough to produce the hexadecimal values 7524595Szliu * shown. 7624595Szliu */ 7724595Szliu 7831853Szliu #if defined(vax)||defined(tahoe) /* VAX D format */ 7931853Szliu #ifdef vax 8031812Szliu #define _0x(A,B) 0x/**/A/**/B 8131853Szliu #else /* vax */ 8231812Szliu #define _0x(A,B) 0x/**/B/**/A 8331853Szliu #endif /* vax */ 8426893Selefunt /* static double */ 8524595Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 8624595Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 8724595Szliu /* lnhuge = 9.4961163736712506989E1 , Hex 2^ 7 * .BDEC1DA73E9010 */ 8824595Szliu /* invln2 = 1.4426950408889634148E0 ; Hex 2^ 1 * .B8AA3B295C17F1 */ 8931812Szliu static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 9031812Szliu static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 9131812Szliu static long lnhugex[] = { _0x(ec1d,43bd), _0x(9010,a73e)}; 9231812Szliu static long invln2x[] = { _0x(aa3b,40b8), _0x(17f1,295c)}; 9324595Szliu #define ln2hi (*(double*)ln2hix) 9424595Szliu #define ln2lo (*(double*)ln2lox) 9524595Szliu #define lnhuge (*(double*)lnhugex) 9624595Szliu #define invln2 (*(double*)invln2x) 9731853Szliu #else /* defined(vax)||defined(tahoe) */ 9826893Selefunt static double 9924595Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 10024595Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 10124595Szliu lnhuge = 7.1602103751842355450E2 , /*Hex 2^ 9 * 1.6602B15B7ECF2 */ 10224595Szliu invln2 = 1.4426950408889633870E0 ; /*Hex 2^ 0 * 1.71547652B82FE */ 10331853Szliu #endif /* defined(vax)||defined(tahoe) */ 10424595Szliu 10524595Szliu double expm1(x) 10624595Szliu double x; 10724595Szliu { 10826893Selefunt static double one=1.0, half=1.0/2.0; 10924595Szliu double scalb(), copysign(), exp__E(), z,hi,lo,c; 11024595Szliu int k,finite(); 11131853Szliu #if defined(vax)||defined(tahoe) 11224595Szliu static prec=56; 11331853Szliu #else /* defined(vax)||defined(tahoe) */ 11424595Szliu static prec=53; 11531853Szliu #endif /* defined(vax)||defined(tahoe) */ 11631853Szliu #if !defined(vax)&&!defined(tahoe) 11724595Szliu if(x!=x) return(x); /* x is NaN */ 11831853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 11924595Szliu 12024595Szliu if( x <= lnhuge ) { 12124595Szliu if( x >= -40.0 ) { 12224595Szliu 12324595Szliu /* argument reduction : x - k*ln2 */ 12424595Szliu k= invln2 *x+copysign(0.5,x); /* k=NINT(x/ln2) */ 12524595Szliu hi=x-k*ln2hi ; 12624595Szliu z=hi-(lo=k*ln2lo); 12724595Szliu c=(hi-z)-lo; 12824595Szliu 12924595Szliu if(k==0) return(z+exp__E(z,c)); 13024595Szliu if(k==1) 13124595Szliu if(z< -0.25) 13224595Szliu {x=z+half;x +=exp__E(z,c); return(x+x);} 13324595Szliu else 13424595Szliu {z+=exp__E(z,c); x=half+z; return(x+x);} 13524595Szliu /* end of k=1 */ 13624595Szliu 13724595Szliu else { 13824595Szliu if(k<=prec) 13924595Szliu { x=one-scalb(one,-k); z += exp__E(z,c);} 14024595Szliu else if(k<100) 14124595Szliu { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;} 14224595Szliu else 14324595Szliu { x = exp__E(z,c)+z; z=one;} 14424595Szliu 14524595Szliu return (scalb(x+z,k)); 14624595Szliu } 14724595Szliu } 14824595Szliu /* end of x > lnunfl */ 14924595Szliu 15024595Szliu else 15124595Szliu /* expm1(-big#) rounded to -1 (inexact) */ 15224595Szliu if(finite(x)) 15324595Szliu { ln2hi+ln2lo; return(-one);} 15424595Szliu 15524595Szliu /* expm1(-INF) is -1 */ 15624595Szliu else return(-one); 15724595Szliu } 15824595Szliu /* end of x < lnhuge */ 15924595Szliu 16024595Szliu else 16124595Szliu /* expm1(INF) is INF, expm1(+big#) overflows to INF */ 16224595Szliu return( finite(x) ? scalb(one,5000) : x); 16324595Szliu } 164