1 /* 2 * Copyright (c) 1985 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * All recipients should regard themselves as participants in an ongoing 8 * research project and hence should feel obligated to report their 9 * experiences (good or bad) with these elementary function codes, using 10 * the sendbug(8) program, to the authors. 11 */ 12 13 #ifndef lint 14 static char sccsid[] = "@(#)log1p.c 5.5 (Berkeley) 06/01/90"; 15 #endif /* not lint */ 16 17 /* LOG1P(x) 18 * RETURN THE LOGARITHM OF 1+x 19 * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS) 20 * CODED IN C BY K.C. NG, 1/19/85; 21 * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85. 22 * 23 * Required system supported functions: 24 * scalb(x,n) 25 * copysign(x,y) 26 * logb(x) 27 * finite(x) 28 * 29 * Required kernel function: 30 * log__L(z) 31 * 32 * Method : 33 * 1. Argument Reduction: find k and f such that 34 * 1+x = 2^k * (1+f), 35 * where sqrt(2)/2 < 1+f < sqrt(2) . 36 * 37 * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 38 * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 39 * log(1+f) is computed by 40 * 41 * log(1+f) = 2s + s*log__L(s*s) 42 * where 43 * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 44 * 45 * See log__L() for the values of the coefficients. 46 * 47 * 3. Finally, log(1+x) = k*ln2 + log(1+f). 48 * 49 * Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers 50 * n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last 51 * 20 bits (for VAX D format), or the last 21 bits ( for IEEE 52 * double) is 0. This ensures n*ln2hi is exactly representable. 53 * 2. In step 1, f may not be representable. A correction term c 54 * for f is computed. It follows that the correction term for 55 * f - t (the leading term of log(1+f) in step 2) is c-c*x. We 56 * add this correction term to n*ln2lo to attenuate the error. 57 * 58 * 59 * Special cases: 60 * log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal; 61 * log1p(INF) is +INF; log1p(-1) is -INF with signal; 62 * only log1p(0)=0 is exact for finite argument. 63 * 64 * Accuracy: 65 * log1p(x) returns the exact log(1+x) nearly rounded. In a test run 66 * with 1,536,000 random arguments on a VAX, the maximum observed 67 * error was .846 ulps (units in the last place). 68 * 69 * Constants: 70 * The hexadecimal values are the intended ones for the following constants. 71 * The decimal values may be used, provided that the compiler will convert 72 * from decimal to binary accurately enough to produce the hexadecimal values 73 * shown. 74 */ 75 76 #include <errno.h> 77 #include "mathimpl.h" 78 79 vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 80 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 81 vc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65) 82 83 ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 84 ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 85 ic(sqrt2, 1.4142135623730951455E0, 0, 1.6A09E667F3BCD) 86 87 #ifdef vccast 88 #define ln2hi vccast(ln2hi) 89 #define ln2lo vccast(ln2lo) 90 #define sqrt2 vccast(sqrt2) 91 #endif 92 93 double log1p(x) 94 double x; 95 { 96 const static double zero=0.0, negone= -1.0, one=1.0, 97 half=1.0/2.0, small=1.0E-20; /* 1+small == 1 */ 98 double z,s,t,c; 99 int k; 100 101 #if !defined(vax)&&!defined(tahoe) 102 if(x!=x) return(x); /* x is NaN */ 103 #endif /* !defined(vax)&&!defined(tahoe) */ 104 105 if(finite(x)) { 106 if( x > negone ) { 107 108 /* argument reduction */ 109 if(copysign(x,one)<small) return(x); 110 k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k); 111 if(z+t >= sqrt2 ) 112 { k += 1 ; z *= half; t *= half; } 113 t += negone; x = z + t; 114 c = (t-x)+z ; /* correction term for x */ 115 116 /* compute log(1+x) */ 117 s = x/(2+x); t = x*x*half; 118 c += (k*ln2lo-c*x); 119 z = c+s*(t+log__L(s*s)); 120 x += (z - t) ; 121 122 return(k*ln2hi+x); 123 } 124 /* end of if (x > negone) */ 125 126 else { 127 #if defined(vax)||defined(tahoe) 128 if ( x == negone ) 129 return (infnan(-ERANGE)); /* -INF */ 130 else 131 return (infnan(EDOM)); /* NaN */ 132 #else /* defined(vax)||defined(tahoe) */ 133 /* x = -1, return -INF with signal */ 134 if ( x == negone ) return( negone/zero ); 135 136 /* negative argument for log, return NaN with signal */ 137 else return ( zero / zero ); 138 #endif /* defined(vax)||defined(tahoe) */ 139 } 140 } 141 /* end of if (finite(x)) */ 142 143 /* log(-INF) is NaN */ 144 else if(x<0) 145 return(zero/zero); 146 147 /* log(+INF) is INF */ 148 else return(x); 149 } 150