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.4 (Berkeley) 09/22/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 #include "mathimpl.h" 79 80 vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 81 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 82 vc(lnhuge, 9.4961163736712506989E1 ,ec1d,43bd,9010,a73e, 7, .BDEC1DA73E9010) 83 vc(invln2, 1.4426950408889634148E0 ,aa3b,40b8,17f1,295c, 1, .B8AA3B295C17F1) 84 85 ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 86 ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 87 ic(lnhuge, 7.1602103751842355450E2, 9, 1.6602B15B7ECF2) 88 ic(invln2, 1.4426950408889633870E0, 0, 1.71547652B82FE) 89 90 #ifdef vccast 91 #define ln2hi vccast(ln2hi) 92 #define ln2lo vccast(ln2lo) 93 #define lnhuge vccast(lnhuge) 94 #define invln2 vccast(invln2) 95 #endif 96 97 double expm1(x) 98 double x; 99 { 100 const static double one=1.0, half=1.0/2.0; 101 double z,hi,lo,c; 102 int k; 103 #if defined(vax)||defined(tahoe) 104 static prec=56; 105 #else /* defined(vax)||defined(tahoe) */ 106 static prec=53; 107 #endif /* defined(vax)||defined(tahoe) */ 108 109 #if !defined(vax)&&!defined(tahoe) 110 if(x!=x) return(x); /* x is NaN */ 111 #endif /* !defined(vax)&&!defined(tahoe) */ 112 113 if( x <= lnhuge ) { 114 if( x >= -40.0 ) { 115 116 /* argument reduction : x - k*ln2 */ 117 k= invln2 *x+copysign(0.5,x); /* k=NINT(x/ln2) */ 118 hi=x-k*ln2hi ; 119 z=hi-(lo=k*ln2lo); 120 c=(hi-z)-lo; 121 122 if(k==0) return(z+exp__E(z,c)); 123 if(k==1) 124 if(z< -0.25) 125 {x=z+half;x +=exp__E(z,c); return(x+x);} 126 else 127 {z+=exp__E(z,c); x=half+z; return(x+x);} 128 /* end of k=1 */ 129 130 else { 131 if(k<=prec) 132 { x=one-scalb(one,-k); z += exp__E(z,c);} 133 else if(k<100) 134 { x = exp__E(z,c)-scalb(one,-k); x+=z; z=one;} 135 else 136 { x = exp__E(z,c)+z; z=one;} 137 138 return (scalb(x+z,k)); 139 } 140 } 141 /* end of x > lnunfl */ 142 143 else 144 /* expm1(-big#) rounded to -1 (inexact) */ 145 if(finite(x)) 146 { ln2hi+ln2lo; return(-one);} 147 148 /* expm1(-INF) is -1 */ 149 else return(-one); 150 } 151 /* end of x < lnhuge */ 152 153 else 154 /* expm1(INF) is INF, expm1(+big#) overflows to INF */ 155 return( finite(x) ? scalb(one,5000) : x); 156 } 157