an update to previous patch fixes nearly all remaining constants used in the math...
[AROS.git] / compiler / stdc / math / ld80 / e_logl.c
blob9ea8f1b7049df5ab5347bbe1047844fbaa9880c6
1 /* $OpenBSD: e_logl.c,v 1.3 2013/11/12 20:35:19 martynas Exp $ */
3 /*
4 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
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.
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.
19 /* logl.c
21 * Natural logarithm, long double precision
25 * SYNOPSIS:
27 * long double x, y, logl();
29 * y = logl( x );
33 * DESCRIPTION:
35 * Returns the base e (2.718...) logarithm of x.
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
41 * log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x).
43 * Otherwise, setting z = 2(x-1)/x+1),
45 * log(x) = z + z**3 P(z)/Q(z).
49 * ACCURACY:
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
56 * In the tests over the interval exp(+-10000), the logarithms
57 * of the random arguments were uniformly distributed over
58 * [-10000, +10000].
60 * ERROR MESSAGES:
62 * log singularity: x = 0; returns -INFINITY
63 * log domain: x < 0; returns NAN
66 #include "math.h"
68 #include "math_private.h"
70 /* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
71 * 1/sqrt(2) <= x < sqrt(2)
72 * Theoretical peak relative error = 2.32e-20
74 static const long double P[] = {
75 4.5270000862445199635215E-5L,
76 4.9854102823193375972212E-1L,
77 6.5787325942061044846969E0L,
78 2.9911919328553073277375E1L,
79 6.0949667980987787057556E1L,
80 5.7112963590585538103336E1L,
81 2.0039553499201281259648E1L,
83 static const long double Q[] = {
84 /* 1.0000000000000000000000E0,*/
85 1.5062909083469192043167E1L,
86 8.3047565967967209469434E1L,
87 2.2176239823732856465394E2L,
88 3.0909872225312059774938E2L,
89 2.1642788614495947685003E2L,
90 6.0118660497603843919306E1L,
93 /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
94 * where z = 2(x-1)/(x+1)
95 * 1/sqrt(2) <= x < sqrt(2)
96 * Theoretical peak relative error = 6.16e-22
99 static const long double R[4] = {
100 1.9757429581415468984296E-3L,
101 -7.1990767473014147232598E-1L,
102 1.0777257190312272158094E1L,
103 -3.5717684488096787370998E1L,
105 static const long double S[4] = {
106 /* 1.00000000000000000000E0L,*/
107 -2.6201045551331104417768E1L,
108 1.9361891836232102174846E2L,
109 -4.2861221385716144629696E2L,
111 static const long double C1 = 6.9314575195312500000000E-1L;
112 static const long double C2 = 1.4286068203094172321215E-6L;
114 #define SQRTH 0.70710678118654752440L
116 long double
117 logl(long double x)
119 long double y, z;
120 int e;
122 if( isnan(x) )
123 return(x);
124 if( x == INFINITY )
125 return(x);
126 /* Test for domain */
127 if( x <= 0.0L )
129 if( x == 0.0L )
130 return( -INFINITY );
131 else
132 return( NAN );
135 /* separate mantissa from exponent */
137 /* Note, frexp is used so that denormal numbers
138 * will be handled properly.
140 x = frexpl( x, &e );
142 /* logarithm using log(x) = z + z**3 P(z)/Q(z),
143 * where z = 2(x-1)/x+1)
145 if( (e > 2) || (e < -2) )
147 if( x < SQRTH )
148 { /* 2( 2x-1 )/( 2x+1 ) */
149 e -= 1;
150 z = x - 0.5L;
151 y = 0.5L * z + 0.5L;
153 else
154 { /* 2 (x-1)/(x+1) */
155 z = x - 0.5L;
156 z -= 0.5L;
157 y = 0.5L * x + 0.5L;
159 x = z / y;
160 z = x*x;
161 z = x * ( z * __polevll( z, (void *)R, 3 ) / __p1evll( z, (void *)S, 3 ) );
162 z = z + e * C2;
163 z = z + x;
164 z = z + e * C1;
165 return( z );
169 /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
171 if( x < SQRTH )
173 e -= 1;
174 x = ldexpl( x, 1 ) - 1.0L; /* 2x - 1 */
176 else
178 x = x - 1.0L;
180 z = x*x;
181 y = x * ( z * __polevll( x, (void *)P, 6 ) / __p1evll( x, (void *)Q, 6 ) );
182 y = y + e * C2;
183 z = y - ldexpl( z, -1 ); /* y - 0.5 * z */
184 /* Note, the sum of above terms does not exceed x/4,
185 * so it contributes at most about 1/4 lsb to the error.
187 z = z + x;
188 z = z + e * C1; /* This sum has an error of 1/2 lsb. */
189 return( z );