xref: /csrg-svn/lib/libm/common_source/log1p.c (revision 24603)
1*24603Szliu /*
2*24603Szliu  * Copyright (c) 1985 Regents of the University of California.
3*24603Szliu  *
4*24603Szliu  * Use and reproduction of this software are granted  in  accordance  with
5*24603Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
6*24603Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
7*24603Szliu  * source, and inclusion of this notice) with the additional understanding
8*24603Szliu  * that  all  recipients  should regard themselves as participants  in  an
9*24603Szliu  * ongoing  research  project and hence should  feel  obligated  to report
10*24603Szliu  * their  experiences (good or bad) with these elementary function  codes,
11*24603Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12*24603Szliu  */
13*24603Szliu 
14*24603Szliu #ifndef lint
15*24603Szliu static char sccsid[] = "@(#)log1p.c	1.1 (ELEFUNT) 09/06/85";
16*24603Szliu #endif not lint
17*24603Szliu 
18*24603Szliu /* LOG1P(x)
19*24603Szliu  * RETURN THE LOGARITHM OF 1+x
20*24603Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS)
21*24603Szliu  * CODED IN C BY K.C. NG, 1/19/85;
22*24603Szliu  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85.
23*24603Szliu  *
24*24603Szliu  * Required system supported functions:
25*24603Szliu  *	scalb(x,n)
26*24603Szliu  *	copysign(x,y)
27*24603Szliu  *	logb(x)
28*24603Szliu  *	finite(x)
29*24603Szliu  *
30*24603Szliu  * Required kernel function:
31*24603Szliu  *	log__L(z)
32*24603Szliu  *
33*24603Szliu  * Method :
34*24603Szliu  *	1. Argument Reduction: find k and f such that
35*24603Szliu  *			1+x  = 2^k * (1+f),
36*24603Szliu  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
37*24603Szliu  *
38*24603Szliu  *	2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
39*24603Szliu  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
40*24603Szliu  *	   log(1+f) is computed by
41*24603Szliu  *
42*24603Szliu  *	     		log(1+f) = 2s + s*log__L(s*s)
43*24603Szliu  *	   where
44*24603Szliu  *		log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...)))
45*24603Szliu  *
46*24603Szliu  *	   See log__L() for the values of the coefficients.
47*24603Szliu  *
48*24603Szliu  *	3. Finally,  log(1+x) = k*ln2 + log(1+f).
49*24603Szliu  *
50*24603Szliu  *	Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers
51*24603Szliu  *		   n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last
52*24603Szliu  *		   20 bits (for VAX D format), or the last 21 bits ( for IEEE
53*24603Szliu  *		   double) is 0. This ensures n*ln2hi is exactly representable.
54*24603Szliu  *		2. In step 1, f may not be representable. A correction term c
55*24603Szliu  *	 	   for f is computed. It follows that the correction term for
56*24603Szliu  *		   f - t (the leading term of log(1+f) in step 2) is c-c*x. We
57*24603Szliu  *		   add this correction term to n*ln2lo to attenuate the error.
58*24603Szliu  *
59*24603Szliu  *
60*24603Szliu  * Special cases:
61*24603Szliu  *	log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal;
62*24603Szliu  *	log1p(INF) is +INF; log1p(-1) is -INF with signal;
63*24603Szliu  *	only log1p(0)=0 is exact for finite argument.
64*24603Szliu  *
65*24603Szliu  * Accuracy:
66*24603Szliu  *	log1p(x) returns the exact log(1+x) nearly rounded. In a test run
67*24603Szliu  *	with 1,536,000 random arguments on a VAX, the maximum observed
68*24603Szliu  *	error was .846 ulps (units in the last place).
69*24603Szliu  *
70*24603Szliu  * Constants:
71*24603Szliu  * The hexadecimal values are the intended ones for the following constants.
72*24603Szliu  * The decimal values may be used, provided that the compiler will convert
73*24603Szliu  * from decimal to binary accurately enough to produce the hexadecimal values
74*24603Szliu  * shown.
75*24603Szliu  */
76*24603Szliu 
77*24603Szliu #ifdef VAX	/* VAX D format */
78*24603Szliu #include <errno.h>
79*24603Szliu 
80*24603Szliu /* double static */
81*24603Szliu /* ln2hi  =  6.9314718055829871446E-1    , Hex  2^  0   *  .B17217F7D00000 */
82*24603Szliu /* ln2lo  =  1.6465949582897081279E-12   , Hex  2^-39   *  .E7BCD5E4F1D9CC */
83*24603Szliu /* sqrt2  =  1.4142135623730950622E0     ; Hex  2^  1   *  .B504F333F9DE65 */
84*24603Szliu static long     ln2hix[] = { 0x72174031, 0x0000f7d0};
85*24603Szliu static long     ln2lox[] = { 0xbcd52ce7, 0xd9cce4f1};
86*24603Szliu static long     sqrt2x[] = { 0x04f340b5, 0xde6533f9};
87*24603Szliu #define    ln2hi    (*(double*)ln2hix)
88*24603Szliu #define    ln2lo    (*(double*)ln2lox)
89*24603Szliu #define    sqrt2    (*(double*)sqrt2x)
90*24603Szliu #else	/* IEEE double */
91*24603Szliu double static
92*24603Szliu ln2hi  =  6.9314718036912381649E-1    , /*Hex  2^ -1   *  1.62E42FEE00000 */
93*24603Szliu ln2lo  =  1.9082149292705877000E-10   , /*Hex  2^-33   *  1.A39EF35793C76 */
94*24603Szliu sqrt2  =  1.4142135623730951455E0     ; /*Hex  2^  0   *  1.6A09E667F3BCD */
95*24603Szliu #endif
96*24603Szliu 
97*24603Szliu double log1p(x)
98*24603Szliu double x;
99*24603Szliu {
100*24603Szliu 	static double zero=0.0, negone= -1.0, one=1.0,
101*24603Szliu 		      half=1.0/2.0, small=1.0E-20;   /* 1+small == 1 */
102*24603Szliu 	double logb(),copysign(),scalb(),log__L(),z,s,t,c;
103*24603Szliu 	int k,finite();
104*24603Szliu 
105*24603Szliu #ifndef VAX
106*24603Szliu 	if(x!=x) return(x);	/* x is NaN */
107*24603Szliu #endif
108*24603Szliu 
109*24603Szliu 	if(finite(x)) {
110*24603Szliu 	   if( x > negone ) {
111*24603Szliu 
112*24603Szliu 	   /* argument reduction */
113*24603Szliu 	      if(copysign(x,one)<small) return(x);
114*24603Szliu 	      k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k);
115*24603Szliu 	      if(z+t >= sqrt2 )
116*24603Szliu 		  { k += 1 ; z *= half; t *= half; }
117*24603Szliu 	      t += negone; x = z + t;
118*24603Szliu 	      c = (t-x)+z ;		/* correction term for x */
119*24603Szliu 
120*24603Szliu  	   /* compute log(1+x)  */
121*24603Szliu               s = x/(2+x); t = x*x*half;
122*24603Szliu 	      c += (k*ln2lo-c*x);
123*24603Szliu 	      z = c+s*(t+log__L(s*s));
124*24603Szliu 	      x += (z - t) ;
125*24603Szliu 
126*24603Szliu 	      return(k*ln2hi+x);
127*24603Szliu 	   }
128*24603Szliu 	/* end of if (x > negone) */
129*24603Szliu 
130*24603Szliu 	    else {
131*24603Szliu #ifdef VAX
132*24603Szliu 		extern double infnan();
133*24603Szliu 		if ( x == negone )
134*24603Szliu 		    return (infnan(-ERANGE));	/* -INF */
135*24603Szliu 		else
136*24603Szliu 		    return (infnan(EDOM));	/* NaN */
137*24603Szliu #else	/* IEEE double */
138*24603Szliu 		/* x = -1, return -INF with signal */
139*24603Szliu 		if ( x == negone ) return( negone/zero );
140*24603Szliu 
141*24603Szliu 		/* negative argument for log, return NaN with signal */
142*24603Szliu 	        else return ( zero / zero );
143*24603Szliu #endif
144*24603Szliu 	    }
145*24603Szliu 	}
146*24603Szliu     /* end of if (finite(x)) */
147*24603Szliu 
148*24603Szliu     /* log(-INF) is NaN */
149*24603Szliu 	else if(x<0)
150*24603Szliu 	     return(zero/zero);
151*24603Szliu 
152*24603Szliu     /* log(+INF) is INF */
153*24603Szliu 	else return(x);
154*24603Szliu }
155