134126Sbostic /* 224603Szliu * Copyright (c) 1985 Regents of the University of California. 334126Sbostic * All rights reserved. 434126Sbostic * 534126Sbostic * Redistribution and use in source and binary forms are permitted 634931Sbostic * provided that the above copyright notice and this paragraph are 734931Sbostic * duplicated in all such forms and that any documentation, 834931Sbostic * advertising materials, and other materials related to such 934931Sbostic * distribution and use acknowledge that the software was developed 1034931Sbostic * by the University of California, Berkeley. The name of the 1134931Sbostic * University may not be used to endorse or promote products derived 1234931Sbostic * from this software without specific prior written permission. 1334931Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434931Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534931Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1634126Sbostic * 1734126Sbostic * All recipients should regard themselves as participants in an ongoing 1834126Sbostic * research project and hence should feel obligated to report their 1934126Sbostic * experiences (good or bad) with these elementary function codes, using 2034126Sbostic * the sendbug(8) program, to the authors. 2124603Szliu */ 2224603Szliu 2324603Szliu #ifndef lint 24*35679Sbostic static char sccsid[] = "@(#)log1p.c 5.4 (Berkeley) 09/22/88"; 2534126Sbostic #endif /* not lint */ 2624603Szliu 2724603Szliu /* LOG1P(x) 2824603Szliu * RETURN THE LOGARITHM OF 1+x 2924603Szliu * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS) 3024603Szliu * CODED IN C BY K.C. NG, 1/19/85; 3124603Szliu * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85. 3224603Szliu * 3324603Szliu * Required system supported functions: 3424603Szliu * scalb(x,n) 3524603Szliu * copysign(x,y) 3624603Szliu * logb(x) 3724603Szliu * finite(x) 3824603Szliu * 3924603Szliu * Required kernel function: 4024603Szliu * log__L(z) 4124603Szliu * 4224603Szliu * Method : 4324603Szliu * 1. Argument Reduction: find k and f such that 4424603Szliu * 1+x = 2^k * (1+f), 4524603Szliu * where sqrt(2)/2 < 1+f < sqrt(2) . 4624603Szliu * 4724603Szliu * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 4824603Szliu * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 4924603Szliu * log(1+f) is computed by 5024603Szliu * 5124603Szliu * log(1+f) = 2s + s*log__L(s*s) 5224603Szliu * where 5324603Szliu * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 5424603Szliu * 5524603Szliu * See log__L() for the values of the coefficients. 5624603Szliu * 5724603Szliu * 3. Finally, log(1+x) = k*ln2 + log(1+f). 5824603Szliu * 5924603Szliu * Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers 6024603Szliu * n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last 6124603Szliu * 20 bits (for VAX D format), or the last 21 bits ( for IEEE 6224603Szliu * double) is 0. This ensures n*ln2hi is exactly representable. 6324603Szliu * 2. In step 1, f may not be representable. A correction term c 6424603Szliu * for f is computed. It follows that the correction term for 6524603Szliu * f - t (the leading term of log(1+f) in step 2) is c-c*x. We 6624603Szliu * add this correction term to n*ln2lo to attenuate the error. 6724603Szliu * 6824603Szliu * 6924603Szliu * Special cases: 7024603Szliu * log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal; 7124603Szliu * log1p(INF) is +INF; log1p(-1) is -INF with signal; 7224603Szliu * only log1p(0)=0 is exact for finite argument. 7324603Szliu * 7424603Szliu * Accuracy: 7524603Szliu * log1p(x) returns the exact log(1+x) nearly rounded. In a test run 7624603Szliu * with 1,536,000 random arguments on a VAX, the maximum observed 7724603Szliu * error was .846 ulps (units in the last place). 7824603Szliu * 7924603Szliu * Constants: 8024603Szliu * The hexadecimal values are the intended ones for the following constants. 8124603Szliu * The decimal values may be used, provided that the compiler will convert 8224603Szliu * from decimal to binary accurately enough to produce the hexadecimal values 8324603Szliu * shown. 8424603Szliu */ 8524603Szliu 8624603Szliu #include <errno.h> 87*35679Sbostic #include "mathimpl.h" 8824603Szliu 89*35679Sbostic vc(ln2hi, 6.9314718055829871446E-1 ,7217,4031,0000,f7d0, 0, .B17217F7D00000) 90*35679Sbostic vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC) 91*35679Sbostic vc(sqrt2, 1.4142135623730950622E0 ,04f3,40b5,de65,33f9, 1, .B504F333F9DE65) 92*35679Sbostic 93*35679Sbostic ic(ln2hi, 6.9314718036912381649E-1, -1, 1.62E42FEE00000) 94*35679Sbostic ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76) 95*35679Sbostic ic(sqrt2, 1.4142135623730951455E0, 0, 1.6A09E667F3BCD) 96*35679Sbostic 97*35679Sbostic #ifdef vccast 98*35679Sbostic #define ln2hi vccast(ln2hi) 99*35679Sbostic #define ln2lo vccast(ln2lo) 100*35679Sbostic #define sqrt2 vccast(sqrt2) 101*35679Sbostic #endif 102*35679Sbostic 10324603Szliu double log1p(x) 10424603Szliu double x; 10524603Szliu { 106*35679Sbostic const static double zero=0.0, negone= -1.0, one=1.0, 10724603Szliu half=1.0/2.0, small=1.0E-20; /* 1+small == 1 */ 108*35679Sbostic double z,s,t,c; 109*35679Sbostic int k; 11024603Szliu 11131853Szliu #if !defined(vax)&&!defined(tahoe) 11224603Szliu if(x!=x) return(x); /* x is NaN */ 11331853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 11424603Szliu 11524603Szliu if(finite(x)) { 11624603Szliu if( x > negone ) { 11724603Szliu 11824603Szliu /* argument reduction */ 11924603Szliu if(copysign(x,one)<small) return(x); 12024603Szliu k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k); 12124603Szliu if(z+t >= sqrt2 ) 12224603Szliu { k += 1 ; z *= half; t *= half; } 12324603Szliu t += negone; x = z + t; 12424603Szliu c = (t-x)+z ; /* correction term for x */ 12524603Szliu 12624603Szliu /* compute log(1+x) */ 12724603Szliu s = x/(2+x); t = x*x*half; 12824603Szliu c += (k*ln2lo-c*x); 12924603Szliu z = c+s*(t+log__L(s*s)); 13024603Szliu x += (z - t) ; 13124603Szliu 13224603Szliu return(k*ln2hi+x); 13324603Szliu } 13424603Szliu /* end of if (x > negone) */ 13524603Szliu 13624603Szliu else { 13731853Szliu #if defined(vax)||defined(tahoe) 13824603Szliu if ( x == negone ) 13924603Szliu return (infnan(-ERANGE)); /* -INF */ 14024603Szliu else 14124603Szliu return (infnan(EDOM)); /* NaN */ 14231853Szliu #else /* defined(vax)||defined(tahoe) */ 14324603Szliu /* x = -1, return -INF with signal */ 14424603Szliu if ( x == negone ) return( negone/zero ); 14524603Szliu 14624603Szliu /* negative argument for log, return NaN with signal */ 14724603Szliu else return ( zero / zero ); 14831853Szliu #endif /* defined(vax)||defined(tahoe) */ 14924603Szliu } 15024603Szliu } 15124603Szliu /* end of if (finite(x)) */ 15224603Szliu 15324603Szliu /* log(-INF) is NaN */ 15424603Szliu else if(x<0) 15524603Szliu return(zero/zero); 15624603Szliu 15724603Szliu /* log(+INF) is INF */ 15824603Szliu else return(x); 15924603Szliu } 160