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