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*31853Szliu "@(#)log.c 4.5 (Berkeley) 8/21/85; 1.6 (ucb.elefunt) 07/13/87"; 17*31853Szliu #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*31853Szliu #if defined(vax)||defined(tahoe) /* VAX D format */ 7124601Szliu #include <errno.h> 72*31853Szliu #ifdef vax 7331812Szliu #define _0x(A,B) 0x/**/A/**/B 74*31853Szliu #else /* vax */ 7531812Szliu #define _0x(A,B) 0x/**/B/**/A 76*31853Szliu #endif /* vax */ 7726893Selefunt /* static double */ 7824601Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 7924601Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 8024601Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 8131812Szliu static long ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)}; 8231812Szliu static long ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)}; 8331812Szliu static long sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)}; 8424601Szliu #define ln2hi (*(double*)ln2hix) 8524601Szliu #define ln2lo (*(double*)ln2lox) 8624601Szliu #define sqrt2 (*(double*)sqrt2x) 87*31853Szliu #else /* defined(vax)||defined(tahoe) */ 8826893Selefunt static double 8924601Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 9024601Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 9124601Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 92*31853Szliu #endif /* defined(vax)||defined(tahoe) */ 9324601Szliu 9424601Szliu double log(x) 9524601Szliu double x; 9624601Szliu { 9724601Szliu static double zero=0.0, negone= -1.0, half=1.0/2.0; 9824601Szliu double logb(),scalb(),copysign(),log__L(),s,z,t; 9924601Szliu int k,n,finite(); 10024601Szliu 101*31853Szliu #if !defined(vax)&&!defined(tahoe) 10224601Szliu if(x!=x) return(x); /* x is NaN */ 103*31853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 10424601Szliu if(finite(x)) { 10524601Szliu if( x > zero ) { 10624601Szliu 10724601Szliu /* argument reduction */ 10824601Szliu k=logb(x); x=scalb(x,-k); 10924601Szliu if(k == -1022) /* subnormal no. */ 11024601Szliu {n=logb(x); x=scalb(x,-n); k+=n;} 11124601Szliu if(x >= sqrt2 ) {k += 1; x *= half;} 11224601Szliu x += negone ; 11324601Szliu 11424601Szliu /* compute log(1+x) */ 11524601Szliu s=x/(2+x); t=x*x*half; 11624601Szliu z=k*ln2lo+s*(t+log__L(s*s)); 11724601Szliu x += (z - t) ; 11824601Szliu 11924601Szliu return(k*ln2hi+x); 12024601Szliu } 12124601Szliu /* end of if (x > zero) */ 12224601Szliu 12324601Szliu else { 124*31853Szliu #if defined(vax)||defined(tahoe) 12524601Szliu extern double infnan(); 12624601Szliu if ( x == zero ) 12724601Szliu return (infnan(-ERANGE)); /* -INF */ 12824601Szliu else 12924601Szliu return (infnan(EDOM)); /* NaN */ 130*31853Szliu #else /* defined(vax)||defined(tahoe) */ 13124601Szliu /* zero argument, return -INF with signal */ 13224601Szliu if ( x == zero ) 13324601Szliu return( negone/zero ); 13424601Szliu 13524601Szliu /* negative argument, return NaN with signal */ 13624601Szliu else 13724601Szliu return ( zero / zero ); 138*31853Szliu #endif /* defined(vax)||defined(tahoe) */ 13924601Szliu } 14024601Szliu } 14124601Szliu /* end of if (finite(x)) */ 142*31853Szliu /* NOTREACHED if defined(vax)||defined(tahoe) */ 14324601Szliu 14424601Szliu /* log(-INF) is NaN with signal */ 14524601Szliu else if (x<0) 14624601Szliu return(zero/zero); 14724601Szliu 14824601Szliu /* log(+INF) is +INF */ 14924601Szliu else return(x); 15024601Szliu 15124601Szliu } 152