1*24601Szliu /* 2*24601Szliu * Copyright (c) 1985 Regents of the University of California. 3*24601Szliu * 4*24601Szliu * Use and reproduction of this software are granted in accordance with 5*24601Szliu * the terms and conditions specified in the Berkeley Software License 6*24601Szliu * Agreement (in particular, this entails acknowledgement of the programs' 7*24601Szliu * source, and inclusion of this notice) with the additional understanding 8*24601Szliu * that all recipients should regard themselves as participants in an 9*24601Szliu * ongoing research project and hence should feel obligated to report 10*24601Szliu * their experiences (good or bad) with these elementary function codes, 11*24601Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 12*24601Szliu */ 13*24601Szliu 14*24601Szliu #ifndef lint 15*24601Szliu static char sccsid[] = "@(#)log.c 1.1 (ELEFUNT) 09/06/85"; 16*24601Szliu #endif not lint 17*24601Szliu 18*24601Szliu /* LOG(X) 19*24601Szliu * RETURN THE LOGARITHM OF x 20*24601Szliu * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS) 21*24601Szliu * CODED IN C BY K.C. NG, 1/19/85; 22*24601Szliu * REVISED BY K.C. NG on 2/7/85, 3/7/85, 3/24/85, 4/16/85. 23*24601Szliu * 24*24601Szliu * Required system supported functions: 25*24601Szliu * scalb(x,n) 26*24601Szliu * copysign(x,y) 27*24601Szliu * logb(x) 28*24601Szliu * finite(x) 29*24601Szliu * 30*24601Szliu * Required kernel function: 31*24601Szliu * log__L(z) 32*24601Szliu * 33*24601Szliu * Method : 34*24601Szliu * 1. Argument Reduction: find k and f such that 35*24601Szliu * x = 2^k * (1+f), 36*24601Szliu * where sqrt(2)/2 < 1+f < sqrt(2) . 37*24601Szliu * 38*24601Szliu * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 39*24601Szliu * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 40*24601Szliu * log(1+f) is computed by 41*24601Szliu * 42*24601Szliu * log(1+f) = 2s + s*log__L(s*s) 43*24601Szliu * where 44*24601Szliu * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 45*24601Szliu * 46*24601Szliu * See log__L() for the values of the coefficients. 47*24601Szliu * 48*24601Szliu * 3. Finally, log(x) = k*ln2 + log(1+f). (Here n*ln2 will be stored 49*24601Szliu * in two floating point number: n*ln2hi + n*ln2lo, n*ln2hi is exact 50*24601Szliu * since the last 20 bits of ln2hi is 0.) 51*24601Szliu * 52*24601Szliu * Special cases: 53*24601Szliu * log(x) is NaN with signal if x < 0 (including -INF) ; 54*24601Szliu * log(+INF) is +INF; log(0) is -INF with signal; 55*24601Szliu * log(NaN) is that NaN with no signal. 56*24601Szliu * 57*24601Szliu * Accuracy: 58*24601Szliu * log(x) returns the exact log(x) nearly rounded. In a test run with 59*24601Szliu * 1,536,000 random arguments on a VAX, the maximum observed error was 60*24601Szliu * .826 ulps (units in the last place). 61*24601Szliu * 62*24601Szliu * Constants: 63*24601Szliu * The hexadecimal values are the intended ones for the following constants. 64*24601Szliu * The decimal values may be used, provided that the compiler will convert 65*24601Szliu * from decimal to binary accurately enough to produce the hexadecimal values 66*24601Szliu * shown. 67*24601Szliu */ 68*24601Szliu 69*24601Szliu #ifdef VAX /* VAX D format */ 70*24601Szliu #include <errno.h> 71*24601Szliu 72*24601Szliu /* double static */ 73*24601Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 74*24601Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 75*24601Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 76*24601Szliu static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 77*24601Szliu static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 78*24601Szliu static long sqrt2x[] = { 0x04f340b5, 0xde6533f9}; 79*24601Szliu #define ln2hi (*(double*)ln2hix) 80*24601Szliu #define ln2lo (*(double*)ln2lox) 81*24601Szliu #define sqrt2 (*(double*)sqrt2x) 82*24601Szliu #else /* IEEE double */ 83*24601Szliu double static 84*24601Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 85*24601Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 86*24601Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 87*24601Szliu #endif 88*24601Szliu 89*24601Szliu double log(x) 90*24601Szliu double x; 91*24601Szliu { 92*24601Szliu static double zero=0.0, negone= -1.0, half=1.0/2.0; 93*24601Szliu double logb(),scalb(),copysign(),log__L(),s,z,t; 94*24601Szliu int k,n,finite(); 95*24601Szliu 96*24601Szliu #ifndef VAX 97*24601Szliu if(x!=x) return(x); /* x is NaN */ 98*24601Szliu #endif 99*24601Szliu if(finite(x)) { 100*24601Szliu if( x > zero ) { 101*24601Szliu 102*24601Szliu /* argument reduction */ 103*24601Szliu k=logb(x); x=scalb(x,-k); 104*24601Szliu if(k == -1022) /* subnormal no. */ 105*24601Szliu {n=logb(x); x=scalb(x,-n); k+=n;} 106*24601Szliu if(x >= sqrt2 ) {k += 1; x *= half;} 107*24601Szliu x += negone ; 108*24601Szliu 109*24601Szliu /* compute log(1+x) */ 110*24601Szliu s=x/(2+x); t=x*x*half; 111*24601Szliu z=k*ln2lo+s*(t+log__L(s*s)); 112*24601Szliu x += (z - t) ; 113*24601Szliu 114*24601Szliu return(k*ln2hi+x); 115*24601Szliu } 116*24601Szliu /* end of if (x > zero) */ 117*24601Szliu 118*24601Szliu else { 119*24601Szliu #ifdef VAX 120*24601Szliu extern double infnan(); 121*24601Szliu if ( x == zero ) 122*24601Szliu return (infnan(-ERANGE)); /* -INF */ 123*24601Szliu else 124*24601Szliu return (infnan(EDOM)); /* NaN */ 125*24601Szliu #else /* IEEE double */ 126*24601Szliu /* zero argument, return -INF with signal */ 127*24601Szliu if ( x == zero ) 128*24601Szliu return( negone/zero ); 129*24601Szliu 130*24601Szliu /* negative argument, return NaN with signal */ 131*24601Szliu else 132*24601Szliu return ( zero / zero ); 133*24601Szliu #endif 134*24601Szliu } 135*24601Szliu } 136*24601Szliu /* end of if (finite(x)) */ 137*24601Szliu /* NOT REACHED ifdef VAX */ 138*24601Szliu 139*24601Szliu /* log(-INF) is NaN with signal */ 140*24601Szliu else if (x<0) 141*24601Szliu return(zero/zero); 142*24601Szliu 143*24601Szliu /* log(+INF) is +INF */ 144*24601Szliu else return(x); 145*24601Szliu 146*24601Szliu } 147