1 /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_logl.c */
3 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Natural logarithm, long double precision
23 * long double x, y, logl();
30 * Returns the base e (2.718...) logarithm of x.
32 * The argument is separated into its exponent and fractional
33 * parts. If the exponent is between -1 and +1, the logarithm
34 * of the fraction is approximated by
36 * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x).
38 * Otherwise, setting z = 2(x-1)/(x+1),
40 * log(x) = log(1+z/2) - log(1-z/2) = z + z**3 P(z)/Q(z).
46 * arithmetic domain # trials peak rms
47 * IEEE 0.5, 2.0 150000 8.71e-20 2.75e-20
48 * IEEE exp(+-10000) 100000 5.39e-20 2.34e-20
50 * In the tests over the interval exp(+-10000), the logarithms
51 * of the random arguments were uniformly distributed over
57 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
58 long double logl(long double x)
62 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
63 /* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
64 * 1/sqrt(2) <= x < sqrt(2)
65 * Theoretical peak relative error = 2.32e-20
67 static const long double P[] = {
68 4.5270000862445199635215E-5L,
69 4.9854102823193375972212E-1L,
70 6.5787325942061044846969E0L,
71 2.9911919328553073277375E1L,
72 6.0949667980987787057556E1L,
73 5.7112963590585538103336E1L,
74 2.0039553499201281259648E1L,
76 static const long double Q[] = {
77 /* 1.0000000000000000000000E0,*/
78 1.5062909083469192043167E1L,
79 8.3047565967967209469434E1L,
80 2.2176239823732856465394E2L,
81 3.0909872225312059774938E2L,
82 2.1642788614495947685003E2L,
83 6.0118660497603843919306E1L,
86 /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
87 * where z = 2(x-1)/(x+1)
88 * 1/sqrt(2) <= x < sqrt(2)
89 * Theoretical peak relative error = 6.16e-22
91 static const long double R[4] = {
92 1.9757429581415468984296E-3L,
93 -7.1990767473014147232598E-1L,
94 1.0777257190312272158094E1L,
95 -3.5717684488096787370998E1L,
97 static const long double S[4] = {
98 /* 1.00000000000000000000E0L,*/
99 -2.6201045551331104417768E1L,
100 1.9361891836232102174846E2L,
101 -4.2861221385716144629696E2L,
103 static const long double C1 = 6.9314575195312500000000E-1L;
104 static const long double C2 = 1.4286068203094172321215E-6L;
106 #define SQRTH 0.70710678118654752440L
108 long double logl(long double x)
119 return -1/(x*x); /* -inf with divbyzero */
120 return 0/0.0f; /* nan with invalid */
123 /* separate mantissa from exponent */
124 /* Note, frexp is used so that denormal numbers
125 * will be handled properly.
129 /* logarithm using log(x) = z + z**3 P(z)/Q(z),
130 * where z = 2(x-1)/(x+1)
132 if (e > 2 || e < -2) {
133 if (x < SQRTH) { /* 2(2x-1)/(2x+1) */
137 } else { /* 2 (x-1)/(x+1) */
144 z = x * (z * __polevll(z, R, 3) / __p1evll(z, S, 3));
151 /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
159 y = x * (z * __polevll(x, P, 6) / __p1evll(x, Q, 6));
162 /* Note, the sum of above terms does not exceed x/4,
163 * so it contributes at most about 1/4 lsb to the error.
166 z = z + e * C1; /* This sum has an error of 1/2 lsb. */
169 #elif LDBL_MANT_DIG == 113 && LDBL_MAX_EXP == 16384
170 // TODO: broken implementation to make things compile
171 long double logl(long double x)