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