xref: /original-bsd/lib/libm/common_source/log1p.c (revision 28301386)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * All recipients should regard themselves as participants in an ongoing
18  * research project and hence should feel obligated to report their
19  * experiences (good or bad) with these elementary function codes, using
20  * the sendbug(8) program, to the authors.
21  */
22 
23 #ifndef lint
24 static char sccsid[] = "@(#)log1p.c	5.4 (Berkeley) 09/22/88";
25 #endif /* not lint */
26 
27 /* LOG1P(x)
28  * RETURN THE LOGARITHM OF 1+x
29  * DOUBLE PRECISION (VAX D FORMAT 56 bits, IEEE DOUBLE 53 BITS)
30  * CODED IN C BY K.C. NG, 1/19/85;
31  * REVISED BY K.C. NG on 2/6/85, 3/7/85, 3/24/85, 4/16/85.
32  *
33  * Required system supported functions:
34  *	scalb(x,n)
35  *	copysign(x,y)
36  *	logb(x)
37  *	finite(x)
38  *
39  * Required kernel function:
40  *	log__L(z)
41  *
42  * Method :
43  *	1. Argument Reduction: find k and f such that
44  *			1+x  = 2^k * (1+f),
45  *	   where  sqrt(2)/2 < 1+f < sqrt(2) .
46  *
47  *	2. Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
48  *		 = 2s + 2/3 s**3 + 2/5 s**5 + .....,
49  *	   log(1+f) is computed by
50  *
51  *	     		log(1+f) = 2s + s*log__L(s*s)
52  *	   where
53  *		log__L(z) = z*(L1 + z*(L2 + z*(... (L6 + z*L7)...)))
54  *
55  *	   See log__L() for the values of the coefficients.
56  *
57  *	3. Finally,  log(1+x) = k*ln2 + log(1+f).
58  *
59  *	Remarks 1. In step 3 n*ln2 will be stored in two floating point numbers
60  *		   n*ln2hi + n*ln2lo, where ln2hi is chosen such that the last
61  *		   20 bits (for VAX D format), or the last 21 bits ( for IEEE
62  *		   double) is 0. This ensures n*ln2hi is exactly representable.
63  *		2. In step 1, f may not be representable. A correction term c
64  *	 	   for f is computed. It follows that the correction term for
65  *		   f - t (the leading term of log(1+f) in step 2) is c-c*x. We
66  *		   add this correction term to n*ln2lo to attenuate the error.
67  *
68  *
69  * Special cases:
70  *	log1p(x) is NaN with signal if x < -1; log1p(NaN) is NaN with no signal;
71  *	log1p(INF) is +INF; log1p(-1) is -INF with signal;
72  *	only log1p(0)=0 is exact for finite argument.
73  *
74  * Accuracy:
75  *	log1p(x) returns the exact log(1+x) nearly rounded. In a test run
76  *	with 1,536,000 random arguments on a VAX, the maximum observed
77  *	error was .846 ulps (units in the last place).
78  *
79  * Constants:
80  * The hexadecimal values are the intended ones for the following constants.
81  * The decimal values may be used, provided that the compiler will convert
82  * from decimal to binary accurately enough to produce the hexadecimal values
83  * shown.
84  */
85 
86 #include <errno.h>
87 #include "mathimpl.h"
88 
89 vc(ln2hi, 6.9314718055829871446E-1  ,7217,4031,0000,f7d0,   0, .B17217F7D00000)
90 vc(ln2lo, 1.6465949582897081279E-12 ,bcd5,2ce7,d9cc,e4f1, -39, .E7BCD5E4F1D9CC)
91 vc(sqrt2, 1.4142135623730950622E0   ,04f3,40b5,de65,33f9,   1, .B504F333F9DE65)
92 
93 ic(ln2hi, 6.9314718036912381649E-1,   -1, 1.62E42FEE00000)
94 ic(ln2lo, 1.9082149292705877000E-10, -33, 1.A39EF35793C76)
95 ic(sqrt2, 1.4142135623730951455E0,     0, 1.6A09E667F3BCD)
96 
97 #ifdef vccast
98 #define	ln2hi	vccast(ln2hi)
99 #define	ln2lo	vccast(ln2lo)
100 #define	sqrt2	vccast(sqrt2)
101 #endif
102 
103 double log1p(x)
104 double x;
105 {
106 	const static double zero=0.0, negone= -1.0, one=1.0,
107 		      half=1.0/2.0, small=1.0E-20;   /* 1+small == 1 */
108 	double z,s,t,c;
109 	int k;
110 
111 #if !defined(vax)&&!defined(tahoe)
112 	if(x!=x) return(x);	/* x is NaN */
113 #endif	/* !defined(vax)&&!defined(tahoe) */
114 
115 	if(finite(x)) {
116 	   if( x > negone ) {
117 
118 	   /* argument reduction */
119 	      if(copysign(x,one)<small) return(x);
120 	      k=logb(one+x); z=scalb(x,-k); t=scalb(one,-k);
121 	      if(z+t >= sqrt2 )
122 		  { k += 1 ; z *= half; t *= half; }
123 	      t += negone; x = z + t;
124 	      c = (t-x)+z ;		/* correction term for x */
125 
126  	   /* compute log(1+x)  */
127               s = x/(2+x); t = x*x*half;
128 	      c += (k*ln2lo-c*x);
129 	      z = c+s*(t+log__L(s*s));
130 	      x += (z - t) ;
131 
132 	      return(k*ln2hi+x);
133 	   }
134 	/* end of if (x > negone) */
135 
136 	    else {
137 #if defined(vax)||defined(tahoe)
138 		if ( x == negone )
139 		    return (infnan(-ERANGE));	/* -INF */
140 		else
141 		    return (infnan(EDOM));	/* NaN */
142 #else	/* defined(vax)||defined(tahoe) */
143 		/* x = -1, return -INF with signal */
144 		if ( x == negone ) return( negone/zero );
145 
146 		/* negative argument for log, return NaN with signal */
147 	        else return ( zero / zero );
148 #endif	/* defined(vax)||defined(tahoe) */
149 	    }
150 	}
151     /* end of if (finite(x)) */
152 
153     /* log(-INF) is NaN */
154 	else if(x<0)
155 	     return(zero/zero);
156 
157     /* log(+INF) is INF */
158 	else return(x);
159 }
160