134126Sbostic /* 224601Szliu * Copyright (c) 1985 Regents of the University of California. 334126Sbostic * All rights reserved. 434126Sbostic * 5*42657Sbostic * %sccs.include.redist.c% 634126Sbostic * 734126Sbostic * All recipients should regard themselves as participants in an ongoing 834126Sbostic * research project and hence should feel obligated to report their 934126Sbostic * experiences (good or bad) with these elementary function codes, using 1034126Sbostic * the sendbug(8) program, to the authors. 1124601Szliu */ 1224601Szliu 1324601Szliu #ifndef lint 14*42657Sbostic static char sccsid[] = "@(#)log.c 5.5 (Berkeley) 06/01/90"; 1534126Sbostic #endif /* not lint */ 1624601Szliu 1724601Szliu /* LOG(X) 1824601Szliu * RETURN THE LOGARITHM OF x 1924601Szliu * DOUBLE PRECISION (VAX D FORMAT 56 bits or IEEE DOUBLE 53 BITS) 2024601Szliu * CODED IN C BY K.C. NG, 1/19/85; 2124601Szliu * REVISED BY K.C. NG on 2/7/85, 3/7/85, 3/24/85, 4/16/85. 2224601Szliu * 2324601Szliu * Required system supported functions: 2424601Szliu * scalb(x,n) 2524601Szliu * copysign(x,y) 2624601Szliu * logb(x) 2724601Szliu * finite(x) 2824601Szliu * 2924601Szliu * Required kernel function: 3024601Szliu * log__L(z) 3124601Szliu * 3224601Szliu * Method : 3324601Szliu * 1. Argument Reduction: find k and f such that 3424601Szliu * x = 2^k * (1+f), 3524601Szliu * where sqrt(2)/2 < 1+f < sqrt(2) . 3624601Szliu * 3724601Szliu * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 3824601Szliu * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 3924601Szliu * log(1+f) is computed by 4024601Szliu * 4124601Szliu * log(1+f) = 2s + s*log__L(s*s) 4224601Szliu * where 4324601Szliu * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 4424601Szliu * 4524601Szliu * See log__L() for the values of the coefficients. 4624601Szliu * 4724601Szliu * 3. Finally, log(x) = k*ln2 + log(1+f). (Here n*ln2 will be stored 4824601Szliu * in two floating point number: n*ln2hi + n*ln2lo, n*ln2hi is exact 4924601Szliu * since the last 20 bits of ln2hi is 0.) 5024601Szliu * 5124601Szliu * Special cases: 5224601Szliu * log(x) is NaN with signal if x < 0 (including -INF) ; 5324601Szliu * log(+INF) is +INF; log(0) is -INF with signal; 5424601Szliu * log(NaN) is that NaN with no signal. 5524601Szliu * 5624601Szliu * Accuracy: 5724601Szliu * log(x) returns the exact log(x) nearly rounded. In a test run with 5824601Szliu * 1,536,000 random arguments on a VAX, the maximum observed error was 5924601Szliu * .826 ulps (units in the last place). 6024601Szliu * 6124601Szliu * Constants: 6224601Szliu * The hexadecimal values are the intended ones for the following constants. 6324601Szliu * The decimal values may be used, provided that the compiler will convert 6424601Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6524601Szliu * shown. 6624601Szliu */ 6724601Szliu 6824601Szliu #include <errno.h> 6935679Sbostic #include "mathimpl.h" 7024601Szliu 7135679Sbostic vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 7235679Sbostic vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 7335679Sbostic vc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65) 7435679Sbostic 7535679Sbostic ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 7635679Sbostic ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 7735679Sbostic ic(sqrt2, 1.4142135623730951455E0, 0, 1.6A09E667F3BCD) 7835679Sbostic 7935679Sbostic #ifdef vccast 8035679Sbostic #define ln2hi vccast(ln2hi) 8135679Sbostic #define ln2lo vccast(ln2lo) 8235679Sbostic #define sqrt2 vccast(sqrt2) 8335679Sbostic #endif 8435679Sbostic 8535679Sbostic 8624601Szliu double log(x) 8724601Szliu double x; 8824601Szliu { 8935679Sbostic const static double zero=0.0, negone= -1.0, half=1.0/2.0; 9035679Sbostic double s,z,t; 9135679Sbostic int k,n; 9224601Szliu 9331853Szliu #if !defined(vax)&&!defined(tahoe) 9424601Szliu if(x!=x) return(x); /* x is NaN */ 9531853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 9624601Szliu if(finite(x)) { 9724601Szliu if( x > zero ) { 9824601Szliu 9924601Szliu /* argument reduction */ 10024601Szliu k=logb(x); x=scalb(x,-k); 10124601Szliu if(k == -1022) /* subnormal no. */ 10224601Szliu {n=logb(x); x=scalb(x,-n); k+=n;} 10324601Szliu if(x >= sqrt2 ) {k += 1; x *= half;} 10424601Szliu x += negone ; 10524601Szliu 10624601Szliu /* compute log(1+x) */ 10724601Szliu s=x/(2+x); t=x*x*half; 10824601Szliu z=k*ln2lo+s*(t+log__L(s*s)); 10924601Szliu x += (z - t) ; 11024601Szliu 11124601Szliu return(k*ln2hi+x); 11224601Szliu } 11324601Szliu /* end of if (x > zero) */ 11424601Szliu 11524601Szliu else { 11631853Szliu #if defined(vax)||defined(tahoe) 11724601Szliu if ( x == zero ) 11824601Szliu return (infnan(-ERANGE)); /* -INF */ 11924601Szliu else 12024601Szliu return (infnan(EDOM)); /* NaN */ 12131853Szliu #else /* defined(vax)||defined(tahoe) */ 12224601Szliu /* zero argument, return -INF with signal */ 12324601Szliu if ( x == zero ) 12424601Szliu return( negone/zero ); 12524601Szliu 12624601Szliu /* negative argument, return NaN with signal */ 12724601Szliu else 12824601Szliu return ( zero / zero ); 12931853Szliu #endif /* defined(vax)||defined(tahoe) */ 13024601Szliu } 13124601Szliu } 13224601Szliu /* end of if (finite(x)) */ 13331853Szliu /* NOTREACHED if defined(vax)||defined(tahoe) */ 13424601Szliu 13524601Szliu /* log(-INF) is NaN with signal */ 13624601Szliu else if (x<0) 13724601Szliu return(zero/zero); 13824601Szliu 13924601Szliu /* log(+INF) is +INF */ 14024601Szliu else return(x); 14124601Szliu 14224601Szliu } 143