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