1 /* $OpenBSD: e_logl.c,v 1.3 2013/11/12 20:35:19 martynas Exp $ */
2
3 /*
4 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 /* logl.c
20 *
21 * Natural logarithm, long double precision
22 *
23 *
24 *
25 * SYNOPSIS:
26 *
27 * long double x, y, logl();
28 *
29 * y = logl( x );
30 *
31 *
32 *
33 * DESCRIPTION:
34 *
35 * Returns the base e (2.718...) logarithm of x.
36 *
37 * The argument is separated into its exponent and fractional
38 * parts. If the exponent is between -1 and +1, the logarithm
39 * of the fraction is approximated by
40 *
41 * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x).
42 *
43 * Otherwise, setting z = 2(x-1)/x+1),
44 *
45 * log(x) = z + z**3 P(z)/Q(z).
46 *
47 *
48 *
49 * ACCURACY:
50 *
51 * Relative error:
52 * arithmetic domain # trials peak rms
53 * IEEE 0.5, 2.0 150000 8.71e-20 2.75e-20
54 * IEEE exp(+-10000) 100000 5.39e-20 2.34e-20
55 *
56 * In the tests over the interval exp(+-10000), the logarithms
57 * of the random arguments were uniformly distributed over
58 * [-10000, +10000].
59 *
60 * ERROR MESSAGES:
61 *
62 * log singularity: x = 0; returns -INFINITY
63 * log domain: x < 0; returns NAN
64 */
65
66
67
68 /* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
69 * 1/sqrt(2) <= x < sqrt(2)
70 * Theoretical peak relative error = 2.32e-20
71 */
72 static long double P[] = {
73 4.5270000862445199635215E-5L,
74 4.9854102823193375972212E-1L,
75 6.5787325942061044846969E0L,
76 2.9911919328553073277375E1L,
77 6.0949667980987787057556E1L,
78 5.7112963590585538103336E1L,
79 2.0039553499201281259648E1L,
80 };
81 static long double Q[] = {
82 /* 1.0000000000000000000000E0,*/
83 1.5062909083469192043167E1L,
84 8.3047565967967209469434E1L,
85 2.2176239823732856465394E2L,
86 3.0909872225312059774938E2L,
87 2.1642788614495947685003E2L,
88 6.0118660497603843919306E1L,
89 };
90
91 /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
92 * where z = 2(x-1)/(x+1)
93 * 1/sqrt(2) <= x < sqrt(2)
94 * Theoretical peak relative error = 6.16e-22
95 */
96
97 static long double R[4] = {
98 1.9757429581415468984296E-3L,
99 -7.1990767473014147232598E-1L,
100 1.0777257190312272158094E1L,
101 -3.5717684488096787370998E1L,
102 };
103 static long double S[4] = {
104 /* 1.00000000000000000000E0L,*/
105 -2.6201045551331104417768E1L,
106 1.9361891836232102174846E2L,
107 -4.2861221385716144629696E2L,
108 };
109 static const long double C1 = 6.9314575195312500000000E-1L;
110 static const long double C2 = 1.4286068203094172321215E-6L;
111
112 #define SQRTH 0.70710678118654752440L
113
114 long double
logl(long double x)115 logl(long double x)
116 {
117 long double y, z;
118 int e;
119
120 if( isnan(x) )
121 return(x + x);
122 if( x == (long double) INFINITY )
123 return(x);
124 /* Test for domain */
125 if( x <= 0.0L )
126 {
127 if( x == 0.0L )
128 return __math_divzerol(1);
129 else
130 return __math_invalidl(x);
131 }
132
133 /* separate mantissa from exponent */
134
135 /* Note, frexp is used so that denormal numbers
136 * will be handled properly.
137 */
138 x = frexpl( x, &e );
139
140 /* logarithm using log(x) = z + z**3 P(z)/Q(z),
141 * where z = 2(x-1)/x+1)
142 */
143 if( (e > 2) || (e < -2) )
144 {
145 if( x < SQRTH )
146 { /* 2( 2x-1 )/( 2x+1 ) */
147 e -= 1;
148 z = x - 0.5L;
149 y = 0.5L * z + 0.5L;
150 }
151 else
152 { /* 2 (x-1)/(x+1) */
153 z = x - 0.5L;
154 z -= 0.5L;
155 y = 0.5L * x + 0.5L;
156 }
157 x = z / y;
158 z = x*x;
159 z = x * ( z * __polevll( z, R, 3 ) / __p1evll( z, S, 3 ) );
160 z = z + e * C2;
161 z = z + x;
162 z = z + e * C1;
163 return( z );
164 }
165
166
167 /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
168
169 if( x < SQRTH )
170 {
171 e -= 1;
172 x = ldexpl( x, 1 ) - 1.0L; /* 2x - 1 */
173 }
174 else
175 {
176 x = x - 1.0L;
177 }
178 z = x*x;
179 y = x * ( z * __polevll( x, P, 6 ) / __p1evll( x, Q, 6 ) );
180 y = y + e * C2;
181 z = y - ldexpl( z, -1 ); /* y - 0.5 * z */
182 /* Note, the sum of above terms does not exceed x/4,
183 * so it contributes at most about 1/4 lsb to the error.
184 */
185 z = z + x;
186 z = z + e * C1; /* This sum has an error of 1/2 lsb. */
187 return( z );
188 }
189