xref: /csrg-svn/lib/libm/common_source/log1p.c (revision 31812)
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
1524706Selefunt static char sccsid[] =
16*31812Szliu "@(#)log1p.c	1.3 (Berkeley) 8/21/85; 1.5 (ucb.elefunt) 07/10/87";
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 
7831790Szliu #if (defined(VAX)||defined(TAHOE))	/* VAX D format */
7924603Szliu #include <errno.h>
80*31812Szliu #ifdef VAX
81*31812Szliu #define _0x(A,B)	0x/**/A/**/B
82*31812Szliu #else	/* VAX */
83*31812Szliu #define _0x(A,B)	0x/**/B/**/A
84*31812Szliu #endif	/* VAX */
8526893Selefunt /* static double */
8624603Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
8724603Szliu /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
8824603Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
89*31812Szliu static long     ln2hix[] = { _0x(7217,4031), _0x(0000,f7d0)};
90*31812Szliu static long     ln2lox[] = { _0x(bcd5,2ce7), _0x(d9cc,e4f1)};
91*31812Szliu static long     sqrt2x[] = { _0x(04f3,40b5), _0x(de65,33f9)};
9224603Szliu #define    ln2hi    (*(double*)ln2hix)
9324603Szliu #define    ln2lo    (*(double*)ln2lox)
9424603Szliu #define    sqrt2    (*(double*)sqrt2x)
9524603Szliu #else	/* IEEE double */
9626893Selefunt static double
9724603Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
9824603Szliu ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
9924603Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
10024603Szliu #endif
10124603Szliu 
10224603Szliu double log1p(x)
10324603Szliu double x;
10424603Szliu {
10524603Szliu 	static double zero=0.0, negone= -1.0, one=1.0,
10624603Szliu 		      half=1.0/2.0, small=1.0E-20;   /* 1+small == 1 */
10724603Szliu 	double logb(),copysign(),scalb(),log__L(),z,s,t,c;
10824603Szliu 	int k,finite();
10924603Szliu 
11031790Szliu #if (!defined(VAX)&&!defined(TAHOE))
11124603Szliu 	if(x!=x) return(x);	/* x is NaN */
11224603Szliu #endif
11324603Szliu 
11424603Szliu 	if(finite(x)) {
11524603Szliu 	   if( x > negone ) {
11624603Szliu 
11724603Szliu 	   /* argument reduction */
11824603Szliu 	      if(copysign(x,one)<small) return(x);
11924603Szliu 	      k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k);
12024603Szliu 	      if(z+t >= sqrt2 )
12124603Szliu 		  { k += 1 ; z *= half; t *= half; }
12224603Szliu 	      t += negone; x = z + t;
12324603Szliu 	      c = (t-x)+z ;		/* correction term for x */
12424603Szliu 
12524603Szliu  	   /* compute log(1+x)  */
12624603Szliu               s = x/(2+x); t = x*x*half;
12724603Szliu 	      c += (k*ln2lo-c*x);
12824603Szliu 	      z = c+s*(t+log__L(s*s));
12924603Szliu 	      x += (z - t) ;
13024603Szliu 
13124603Szliu 	      return(k*ln2hi+x);
13224603Szliu 	   }
13324603Szliu 	/* end of if (x > negone) */
13424603Szliu 
13524603Szliu 	    else {
13631790Szliu #if (defined(VAX)||defined(TAHOE))
13724603Szliu 		extern double infnan();
13824603Szliu 		if ( x == negone )
13924603Szliu 		    return (infnan(-ERANGE));	/* -INF */
14024603Szliu 		else
14124603Szliu 		    return (infnan(EDOM));	/* NaN */
14224603Szliu #else	/* IEEE double */
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 );
14824603Szliu #endif
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