124603Szliu /* 224603Szliu * Copyright (c) 1985 Regents of the University of California. 324603Szliu * 424603Szliu * Use and reproduction of this software are granted in accordance with 524603Szliu * the terms and conditions specified in the Berkeley Software License 624603Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724603Szliu * source, and inclusion of this notice) with the additional understanding 824603Szliu * that all recipients should regard themselves as participants in an 924603Szliu * ongoing research project and hence should feel obligated to report 1024603Szliu * their experiences (good or bad) with these elementary function codes, 1124603Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224603Szliu */ 1324603Szliu 1424603Szliu #ifndef lint 15*24706Selefunt static char sccsid[] = 16*24706Selefunt "@(#)log1p.c 1.3 (Berkeley) 8/21/85; 1.2 (ucb.elefunt) 09/11/85"; 1724603Szliu #endif not lint 1824603Szliu 1924603Szliu /* LOG1P(x) 2024603Szliu * RETURN THE LOGARITHM OF 1+x 2124603Szliu * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS) 2224603Szliu * CODED IN C BY K.C. NG, 1/19/85; 2324603Szliu * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85. 2424603Szliu * 2524603Szliu * Required system supported functions: 2624603Szliu * scalb(x,n) 2724603Szliu * copysign(x,y) 2824603Szliu * logb(x) 2924603Szliu * finite(x) 3024603Szliu * 3124603Szliu * Required kernel function: 3224603Szliu * log__L(z) 3324603Szliu * 3424603Szliu * Method : 3524603Szliu * 1. Argument Reduction: find k and f such that 3624603Szliu * 1+x = 2^k * (1+f), 3724603Szliu * where sqrt(2)/2 < 1+f < sqrt(2) . 3824603Szliu * 3924603Szliu * 2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s) 4024603Szliu * = 2s + 2/3 s**3 + 2/5 s**5 + ....., 4124603Szliu * log(1+f) is computed by 4224603Szliu * 4324603Szliu * log(1+f) = 2s + s*log__L(s*s) 4424603Szliu * where 4524603Szliu * log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...))) 4624603Szliu * 4724603Szliu * See log__L() for the values of the coefficients. 4824603Szliu * 4924603Szliu * 3. Finally, log(1+x) = k*ln2 + log(1+f). 5024603Szliu * 5124603Szliu * Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers 5224603Szliu * n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last 5324603Szliu * 20 bits (for VAX D format), or the last 21 bits ( for IEEE 5424603Szliu * double) is 0. This ensures n*ln2hi is exactly representable. 5524603Szliu * 2. In step 1, f may not be representable. A correction term c 5624603Szliu * for f is computed. It follows that the correction term for 5724603Szliu * f - t (the leading term of log(1+f) in step 2) is c-c*x. We 5824603Szliu * add this correction term to n*ln2lo to attenuate the error. 5924603Szliu * 6024603Szliu * 6124603Szliu * Special cases: 6224603Szliu * log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal; 6324603Szliu * log1p(INF) is +INF; log1p(-1) is -INF with signal; 6424603Szliu * only log1p(0)=0 is exact for finite argument. 6524603Szliu * 6624603Szliu * Accuracy: 6724603Szliu * log1p(x) returns the exact log(1+x) nearly rounded. In a test run 6824603Szliu * with 1,536,000 random arguments on a VAX, the maximum observed 6924603Szliu * error was .846 ulps (units in the last place). 7024603Szliu * 7124603Szliu * Constants: 7224603Szliu * The hexadecimal values are the intended ones for the following constants. 7324603Szliu * The decimal values may be used, provided that the compiler will convert 7424603Szliu * from decimal to binary accurately enough to produce the hexadecimal values 7524603Szliu * shown. 7624603Szliu */ 7724603Szliu 7824603Szliu #ifdef VAX /* VAX D format */ 7924603Szliu #include <errno.h> 8024603Szliu 8124603Szliu /* double static */ 8224603Szliu /* ln2hi = 6.9314718055829871446E-1 , Hex 2^ 0 * .B17217F7D00000 */ 8324603Szliu /* ln2lo = 1.6465949582897081279E-12 , Hex 2^-39 * .E7BCD5E4F1D9CC */ 8424603Szliu /* sqrt2 = 1.4142135623730950622E0 ; Hex 2^ 1 * .B504F333F9DE65 */ 8524603Szliu static long ln2hix[] = { 0x72174031, 0x0000f7d0}; 8624603Szliu static long ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1}; 8724603Szliu static long sqrt2x[] = { 0x04f340b5, 0xde6533f9}; 8824603Szliu #define ln2hi (*(double*)ln2hix) 8924603Szliu #define ln2lo (*(double*)ln2lox) 9024603Szliu #define sqrt2 (*(double*)sqrt2x) 9124603Szliu #else /* IEEE double */ 9224603Szliu double static 9324603Szliu ln2hi = 6.9314718036912381649E-1 , /*Hex 2^ -1 * 1.62E42FEE00000 */ 9424603Szliu ln2lo = 1.9082149292705877000E-10 , /*Hex 2^-33 * 1.A39EF35793C76 */ 9524603Szliu sqrt2 = 1.4142135623730951455E0 ; /*Hex 2^ 0 * 1.6A09E667F3BCD */ 9624603Szliu #endif 9724603Szliu 9824603Szliu double log1p(x) 9924603Szliu double x; 10024603Szliu { 10124603Szliu static double zero=0.0, negone= -1.0, one=1.0, 10224603Szliu half=1.0/2.0, small=1.0E-20; /* 1+small == 1 */ 10324603Szliu double logb(),copysign(),scalb(),log__L(),z,s,t,c; 10424603Szliu int k,finite(); 10524603Szliu 10624603Szliu #ifndef VAX 10724603Szliu if(x!=x) return(x); /* x is NaN */ 10824603Szliu #endif 10924603Szliu 11024603Szliu if(finite(x)) { 11124603Szliu if( x > negone ) { 11224603Szliu 11324603Szliu /* argument reduction */ 11424603Szliu if(copysign(x,one)<small) return(x); 11524603Szliu k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k); 11624603Szliu if(z+t >= sqrt2 ) 11724603Szliu { k += 1 ; z *= half; t *= half; } 11824603Szliu t += negone; x = z + t; 11924603Szliu c = (t-x)+z ; /* correction term for x */ 12024603Szliu 12124603Szliu /* compute log(1+x) */ 12224603Szliu s = x/(2+x); t = x*x*half; 12324603Szliu c += (k*ln2lo-c*x); 12424603Szliu z = c+s*(t+log__L(s*s)); 12524603Szliu x += (z - t) ; 12624603Szliu 12724603Szliu return(k*ln2hi+x); 12824603Szliu } 12924603Szliu /* end of if (x > negone) */ 13024603Szliu 13124603Szliu else { 13224603Szliu #ifdef VAX 13324603Szliu extern double infnan(); 13424603Szliu if ( x == negone ) 13524603Szliu return (infnan(-ERANGE)); /* -INF */ 13624603Szliu else 13724603Szliu return (infnan(EDOM)); /* NaN */ 13824603Szliu #else /* IEEE double */ 13924603Szliu /* x = -1, return -INF with signal */ 14024603Szliu if ( x == negone ) return( negone/zero ); 14124603Szliu 14224603Szliu /* negative argument for log, return NaN with signal */ 14324603Szliu else return ( zero / zero ); 14424603Szliu #endif 14524603Szliu } 14624603Szliu } 14724603Szliu /* end of if (finite(x)) */ 14824603Szliu 14924603Szliu /* log(-INF) is NaN */ 15024603Szliu else if(x<0) 15124603Szliu return(zero/zero); 15224603Szliu 15324603Szliu /* log(+INF) is INF */ 15424603Szliu else return(x); 15524603Szliu } 156