134126Sbostic /* 2*61309Sbostic * Copyright (c) 1985, 1993 3*61309Sbostic * The Regents of the University of California. All rights reserved. 434126Sbostic * 542657Sbostic * %sccs.include.redist.c% 624603Szliu */ 724603Szliu 824603Szliu #ifndef lint 9*61309Sbostic static char sccsid[] = "@(#)log1p.c 8.1 (Berkeley) 06/04/93"; 1034126Sbostic #endif /* not lint */ 1124603Szliu 1224603Szliu /* LOG1P(x) 1324603Szliu * RETURN THE LOGARITHM OF 1+x 1424603Szliu * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS) 1524603Szliu * CODED IN C BY K.C. NG, 1/19/85; 1624603Szliu * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85. 1724603Szliu * 1824603Szliu * Required system supported functions: 1924603Szliu * scalb(x,n) 2024603Szliu * copysign(x,y) 2124603Szliu * logb(x) 2224603Szliu * finite(x) 2324603Szliu * 2424603Szliu * Required kernel function: 2524603Szliu * log__L(z) 2624603Szliu * 2724603Szliu * Method : 2824603Szliu * 1. Argument Reduction: find k and f such that 2924603Szliu * 1+x = 2^k * (1+f), 3024603Szliu * where sqrt(2)/2 < 1+f < sqrt(2) . 3124603Szliu * 3224603Szliu * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 3324603Szliu * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 3424603Szliu * log(1+f) is computed by 3524603Szliu * 3624603Szliu * log(1+f) = 2s + s*log__L(s*s) 3724603Szliu * where 3824603Szliu * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 3924603Szliu * 4024603Szliu * See log__L() for the values of the coefficients. 4124603Szliu * 4224603Szliu * 3. Finally, log(1+x) = k*ln2 + log(1+f). 4324603Szliu * 4424603Szliu * Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers 4524603Szliu * n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last 4624603Szliu * 20 bits (for VAX D format), or the last 21 bits ( for IEEE 4724603Szliu * double) is 0. This ensures n*ln2hi is exactly representable. 4824603Szliu * 2. In step 1, f may not be representable. A correction term c 4924603Szliu * for f is computed. It follows that the correction term for 5024603Szliu * f - t (the leading term of log(1+f) in step 2) is c-c*x. We 5124603Szliu * add this correction term to n*ln2lo to attenuate the error. 5224603Szliu * 5324603Szliu * 5424603Szliu * Special cases: 5524603Szliu * log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal; 5624603Szliu * log1p(INF) is +INF; log1p(-1) is -INF with signal; 5724603Szliu * only log1p(0)=0 is exact for finite argument. 5824603Szliu * 5924603Szliu * Accuracy: 6024603Szliu * log1p(x) returns the exact log(1+x) nearly rounded. In a test run 6124603Szliu * with 1,536,000 random arguments on a VAX, the maximum observed 6224603Szliu * error was .846 ulps (units in the last place). 6324603Szliu * 6424603Szliu * Constants: 6524603Szliu * The hexadecimal values are the intended ones for the following constants. 6624603Szliu * The decimal values may be used, provided that the compiler will convert 6724603Szliu * from decimal to binary accurately enough to produce the hexadecimal values 6824603Szliu * shown. 6924603Szliu */ 7024603Szliu 7124603Szliu #include <errno.h> 7235679Sbostic #include "mathimpl.h" 7324603Szliu 7435679Sbostic vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 7535679Sbostic vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 7635679Sbostic vc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65) 7735679Sbostic 7835679Sbostic ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 7935679Sbostic ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 8035679Sbostic ic(sqrt2, 1.4142135623730951455E0, 0, 1.6A09E667F3BCD) 8135679Sbostic 8235679Sbostic #ifdef vccast 8335679Sbostic #define ln2hi vccast(ln2hi) 8435679Sbostic #define ln2lo vccast(ln2lo) 8535679Sbostic #define sqrt2 vccast(sqrt2) 8635679Sbostic #endif 8735679Sbostic 8824603Szliu double log1p(x) 8924603Szliu double x; 9024603Szliu { 9135679Sbostic const static double zero=0.0, negone= -1.0, one=1.0, 9224603Szliu half=1.0/2.0, small=1.0E-20; /* 1+small == 1 */ 9335679Sbostic double z,s,t,c; 9435679Sbostic int k; 9524603Szliu 9631853Szliu #if !defined(vax)&&!defined(tahoe) 9724603Szliu if(x!=x) return(x); /* x is NaN */ 9831853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 9924603Szliu 10024603Szliu if(finite(x)) { 10124603Szliu if( x > negone ) { 10224603Szliu 10324603Szliu /* argument reduction */ 10424603Szliu if(copysign(x,one)<small) return(x); 10524603Szliu k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k); 10624603Szliu if(z+t >= sqrt2 ) 10724603Szliu { k += 1 ; z *= half; t *= half; } 10824603Szliu t += negone; x = z + t; 10924603Szliu c = (t-x)+z ; /* correction term for x */ 11024603Szliu 11124603Szliu /* compute log(1+x) */ 11224603Szliu s = x/(2+x); t = x*x*half; 11324603Szliu c += (k*ln2lo-c*x); 11457452Sbostic z = c+s*(t+__log__L(s*s)); 11524603Szliu x += (z - t) ; 11624603Szliu 11724603Szliu return(k*ln2hi+x); 11824603Szliu } 11924603Szliu /* end of if (x > negone) */ 12024603Szliu 12124603Szliu else { 12231853Szliu #if defined(vax)||defined(tahoe) 12324603Szliu if ( x == negone ) 12424603Szliu return (infnan(-ERANGE)); /* -INF */ 12524603Szliu else 12624603Szliu return (infnan(EDOM)); /* NaN */ 12731853Szliu #else /* defined(vax)||defined(tahoe) */ 12824603Szliu /* x = -1, return -INF with signal */ 12924603Szliu if ( x == negone ) return( negone/zero ); 13024603Szliu 13124603Szliu /* negative argument for log, return NaN with signal */ 13224603Szliu else return ( zero / zero ); 13331853Szliu #endif /* defined(vax)||defined(tahoe) */ 13424603Szliu } 13524603Szliu } 13624603Szliu /* end of if (finite(x)) */ 13724603Szliu 13824603Szliu /* log(-INF) is NaN */ 13924603Szliu else if(x<0) 14024603Szliu return(zero/zero); 14124603Szliu 14224603Szliu /* log(+INF) is INF */ 14324603Szliu else return(x); 14424603Szliu } 145