xref: /original-bsd/lib/libm/common_source/log1p.c (revision c3e32dec)
1 /*
2  * Copyright (c) 1985, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)log1p.c	8.1 (Berkeley) 06/04/93";
10 #endif /* not lint */
11 
12 /* LOG1P(x)
13  * RETURN THE LOGARITHM OF 1+x
14  * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS)
15  * CODED IN C BY K.C. NG, 1/19/85;
16  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85.
17  *
18  * Required system supported functions:
19  *	scalb(x,n)
20  *	copysign(x,y)
21  *	logb(x)
22  *	finite(x)
23  *
24  * Required kernel function:
25  *	log__L(z)
26  *
27  * Method :
28  *	1. Argument Reduction: find k and f such that
29  *			1+x  = 2^k * (1+f),
30  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
31  *
32  *	2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
33  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
34  *	   log(1+f) is computed by
35  *
36  *	     		log(1+f) = 2s + s*log__L(s*s)
37  *	   where
38  *		log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...)))
39  *
40  *	   See log__L() for the values of the coefficients.
41  *
42  *	3. Finally,  log(1+x) = k*ln2 + log(1+f).
43  *
44  *	Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers
45  *		   n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last
46  *		   20 bits (for VAX D format), or the last 21 bits ( for IEEE
47  *		   double) is 0. This ensures n*ln2hi is exactly representable.
48  *		2. In step 1, f may not be representable. A correction term c
49  *	 	   for f is computed. It follows that the correction term for
50  *		   f - t (the leading term of log(1+f) in step 2) is c-c*x. We
51  *		   add this correction term to n*ln2lo to attenuate the error.
52  *
53  *
54  * Special cases:
55  *	log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal;
56  *	log1p(INF) is +INF; log1p(-1) is -INF with signal;
57  *	only log1p(0)=0 is exact for finite argument.
58  *
59  * Accuracy:
60  *	log1p(x) returns the exact log(1+x) nearly rounded. In a test run
61  *	with 1,536,000 random arguments on a VAX, the maximum observed
62  *	error was .846 ulps (units in the last place).
63  *
64  * Constants:
65  * The hexadecimal values are the intended ones for the following constants.
66  * The decimal values may be used, provided that the compiler will convert
67  * from decimal to binary accurately enough to produce the hexadecimal values
68  * shown.
69  */
70 
71 #include <errno.h>
72 #include "mathimpl.h"
73 
74 vc(ln2hi, 6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
75 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
76 vc(sqrt2, 1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
77 
78 ic(ln2hi, 6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
79 ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76)
80 ic(sqrt2, 1.4142135623730951455E0,     0, 1.6A09E667F3BCD)
81 
82 #ifdef vccast
83 #define	ln2hi	vccast(ln2hi)
84 #define	ln2lo	vccast(ln2lo)
85 #define	sqrt2	vccast(sqrt2)
86 #endif
87 
88 double log1p(x)
89 double x;
90 {
91 	const static double zero=0.0, negone= -1.0, one=1.0,
92 		      half=1.0/2.0, small=1.0E-20;   /* 1+small == 1 */
93 	double z,s,t,c;
94 	int k;
95 
96 #if !defined(vax)&&!defined(tahoe)
97 	if(x!=x) return(x);	/* x is NaN */
98 #endif	/* !defined(vax)&&!defined(tahoe) */
99 
100 	if(finite(x)) {
101 	   if( x > negone ) {
102 
103 	   /* argument reduction */
104 	      if(copysign(x,one)<small) return(x);
105 	      k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k);
106 	      if(z+t >= sqrt2 )
107 		  { k += 1 ; z *= half; t *= half; }
108 	      t += negone; x = z + t;
109 	      c = (t-x)+z ;		/* correction term for x */
110 
111  	   /* compute log(1+x)  */
112               s = x/(2+x); t = x*x*half;
113 	      c += (k*ln2lo-c*x);
114 	      z = c+s*(t+__log__L(s*s));
115 	      x += (z - t) ;
116 
117 	      return(k*ln2hi+x);
118 	   }
119 	/* end of if (x > negone) */
120 
121 	    else {
122 #if defined(vax)||defined(tahoe)
123 		if ( x == negone )
124 		    return (infnan(-ERANGE));	/* -INF */
125 		else
126 		    return (infnan(EDOM));	/* NaN */
127 #else	/* defined(vax)||defined(tahoe) */
128 		/* x = -1, return -INF with signal */
129 		if ( x == negone ) return( negone/zero );
130 
131 		/* negative argument for log, return NaN with signal */
132 	        else return ( zero / zero );
133 #endif	/* defined(vax)||defined(tahoe) */
134 	    }
135 	}
136     /* end of if (finite(x)) */
137 
138     /* log(-INF) is NaN */
139 	else if(x<0)
140 	     return(zero/zero);
141 
142     /* log(+INF) is INF */
143 	else return(x);
144 }
145