124601Szliu /* 224601Szliu * Copyright (c) 1985 Regents of the University of California. 324601Szliu * 424601Szliu * Use and reproduction of this software are granted in accordance with 524601Szliu * the terms and conditions specified in the Berkeley Software License 624601Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724601Szliu * source, and inclusion of this notice) with the additional understanding 824601Szliu * that all recipients should regard themselves as participants in an 924601Szliu * ongoing research project and hence should feel obligated to report 1024601Szliu * their experiences (good or bad) with these elementary function codes, 1124601Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224601Szliu */ 1324601Szliu 1424601Szliu #ifndef lint 1524706Selefunt static char sccsid[] = 16*31790Szliu "@(#)log.c 4.5 (Berkeley) 8/21/85; 1.4 (ucb.elefunt) 07/07/87"; 1724601Szliu #endif not lint 1824601Szliu 1924601Szliu /* LOG(X) 2024601Szliu * RETURN THE LOGARITHM OF x 2124601Szliu * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS) 2224601Szliu * CODED IN C BY K.C. NG, 1/19/85; 2324601Szliu * REVISED BY K.C. NG on 2/7/85, 3/7/85, 3/24/85, 4/16/85. 2424601Szliu * 2524601Szliu * Required system supported functions: 2624601Szliu * scalb(x,n) 2724601Szliu * copysign(x,y) 2824601Szliu * logb(x) 2924601Szliu * finite(x) 3024601Szliu * 3124601Szliu * Required kernel function: 3224601Szliu * log__L(z) 3324601Szliu * 3424601Szliu * Method : 3524601Szliu * 1. Argument Reduction: find k and f such that 3624601Szliu * x = 2^k * (1+f), 3724601Szliu * where sqrt(2)/2 < 1+f < sqrt(2) . 3824601Szliu * 3924601Szliu * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 4024601Szliu * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 4124601Szliu * log(1+f) is computed by 4224601Szliu * 4324601Szliu * log(1+f) = 2s + s*log__L(s*s) 4424601Szliu * where 4524601Szliu * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 4624601Szliu * 4724601Szliu * See log__L() for the values of the coefficients. 4824601Szliu * 4924601Szliu * 3. Finally, log(x) = k*ln2 + log(1+f). (Here n*ln2 will be stored 5024601Szliu * in two floating point number: n*ln2hi + n*ln2lo, n*ln2hi is exact 5124601Szliu * since the last 20 bits of ln2hi is 0.) 5224601Szliu * 5324601Szliu * Special cases: 5424601Szliu * log(x) is NaN with signal if x < 0 (including -INF) ; 5524601Szliu * log(+INF) is +INF; log(0) is -INF with signal; 5624601Szliu * log(NaN) is that NaN with no signal. 5724601Szliu * 5824601Szliu * Accuracy: 5924601Szliu * log(x) returns the exact log(x) nearly rounded. In a test run with 6024601Szliu * 1,536,000 random arguments on a VAX, the maximum observed error was 6124601Szliu * .826 ulps (units in the last place). 6224601Szliu * 6324601Szliu * Constants: 6424601Szliu * The hexadecimal values are the intended ones for the following constants. 6524601Szliu * The decimal values may be used, provided that the compiler will convert 6624601Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6724601Szliu * shown. 6824601Szliu */ 6924601Szliu 70*31790Szliu #if (defined(VAX)||defined(TAHOE)) /* VAX D format */ 7124601Szliu #include <errno.h> 7224601Szliu 7326893Selefunt /* static double */ 7424601Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 7524601Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 7624601Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 7724601Szliu static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 7824601Szliu static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 7924601Szliu static long sqrt2x[] = { 0x04f340b5, 0xde6533f9}; 8024601Szliu #define ln2hi (*(double*)ln2hix) 8124601Szliu #define ln2lo (*(double*)ln2lox) 8224601Szliu #define sqrt2 (*(double*)sqrt2x) 8324601Szliu #else /* IEEE double */ 8426893Selefunt static double 8524601Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 8624601Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 8724601Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 8824601Szliu #endif 8924601Szliu 9024601Szliu double log(x) 9124601Szliu double x; 9224601Szliu { 9324601Szliu static double zero=0.0, negone= -1.0, half=1.0/2.0; 9424601Szliu double logb(),scalb(),copysign(),log__L(),s,z,t; 9524601Szliu int k,n,finite(); 9624601Szliu 97*31790Szliu #if (!defined(VAX)&&!defined(TAHOE)) 9824601Szliu if(x!=x) return(x); /* x is NaN */ 9924601Szliu #endif 10024601Szliu if(finite(x)) { 10124601Szliu if( x > zero ) { 10224601Szliu 10324601Szliu /* argument reduction */ 10424601Szliu k=logb(x); x=scalb(x,-k); 10524601Szliu if(k == -1022) /* subnormal no. */ 10624601Szliu {n=logb(x); x=scalb(x,-n); k+=n;} 10724601Szliu if(x >= sqrt2 ) {k += 1; x *= half;} 10824601Szliu x += negone ; 10924601Szliu 11024601Szliu /* compute log(1+x) */ 11124601Szliu s=x/(2+x); t=x*x*half; 11224601Szliu z=k*ln2lo+s*(t+log__L(s*s)); 11324601Szliu x += (z - t) ; 11424601Szliu 11524601Szliu return(k*ln2hi+x); 11624601Szliu } 11724601Szliu /* end of if (x > zero) */ 11824601Szliu 11924601Szliu else { 120*31790Szliu #if (defined(VAX)||defined(TAHOE)) 12124601Szliu extern double infnan(); 12224601Szliu if ( x == zero ) 12324601Szliu return (infnan(-ERANGE)); /* -INF */ 12424601Szliu else 12524601Szliu return (infnan(EDOM)); /* NaN */ 12624601Szliu #else /* IEEE double */ 12724601Szliu /* zero argument, return -INF with signal */ 12824601Szliu if ( x == zero ) 12924601Szliu return( negone/zero ); 13024601Szliu 13124601Szliu /* negative argument, return NaN with signal */ 13224601Szliu else 13324601Szliu return ( zero / zero ); 13424601Szliu #endif 13524601Szliu } 13624601Szliu } 13724601Szliu /* end of if (finite(x)) */ 13824601Szliu /* NOT REACHED ifdef VAX */ 13924601Szliu 14024601Szliu /* log(-INF) is NaN with signal */ 14124601Szliu else if (x<0) 14224601Szliu return(zero/zero); 14324601Szliu 14424601Szliu /* log(+INF) is +INF */ 14524601Szliu else return(x); 14624601Szliu 14724601Szliu } 148