xref: /openbsd-src/lib/libm/src/ld80/e_logl.c (revision beb15867ad58c144f0b15621aba82cb13678cd97)
1*beb15867Skrw /*	$OpenBSD: e_logl.c,v 1.5 2017/01/21 08:29:13 krw Exp $	*/
249393c00Smartynas 
349393c00Smartynas /*
449393c00Smartynas  * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
549393c00Smartynas  *
649393c00Smartynas  * Permission to use, copy, modify, and distribute this software for any
749393c00Smartynas  * purpose with or without fee is hereby granted, provided that the above
849393c00Smartynas  * copyright notice and this permission notice appear in all copies.
949393c00Smartynas  *
1049393c00Smartynas  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1149393c00Smartynas  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1249393c00Smartynas  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1349393c00Smartynas  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1449393c00Smartynas  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1549393c00Smartynas  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1649393c00Smartynas  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1749393c00Smartynas  */
1849393c00Smartynas 
1949393c00Smartynas /*							logl.c
2049393c00Smartynas  *
2149393c00Smartynas  *	Natural logarithm, long double precision
2249393c00Smartynas  *
2349393c00Smartynas  *
2449393c00Smartynas  *
2549393c00Smartynas  * SYNOPSIS:
2649393c00Smartynas  *
2749393c00Smartynas  * long double x, y, logl();
2849393c00Smartynas  *
2949393c00Smartynas  * y = logl( x );
3049393c00Smartynas  *
3149393c00Smartynas  *
3249393c00Smartynas  *
3349393c00Smartynas  * DESCRIPTION:
3449393c00Smartynas  *
3549393c00Smartynas  * Returns the base e (2.718...) logarithm of x.
3649393c00Smartynas  *
3749393c00Smartynas  * The argument is separated into its exponent and fractional
3849393c00Smartynas  * parts.  If the exponent is between -1 and +1, the logarithm
3949393c00Smartynas  * of the fraction is approximated by
4049393c00Smartynas  *
4149393c00Smartynas  *     log(1+x) = x - 0.5 x**2 + x**3 P(x)/Q(x).
4249393c00Smartynas  *
4349393c00Smartynas  * Otherwise, setting  z = 2(x-1)/x+1),
4449393c00Smartynas  *
4549393c00Smartynas  *     log(x) = z + z**3 P(z)/Q(z).
4649393c00Smartynas  *
4749393c00Smartynas  *
4849393c00Smartynas  *
4949393c00Smartynas  * ACCURACY:
5049393c00Smartynas  *
5149393c00Smartynas  *                      Relative error:
5249393c00Smartynas  * arithmetic   domain     # trials      peak         rms
5349393c00Smartynas  *    IEEE      0.5, 2.0    150000      8.71e-20    2.75e-20
5449393c00Smartynas  *    IEEE     exp(+-10000) 100000      5.39e-20    2.34e-20
5549393c00Smartynas  *
5649393c00Smartynas  * In the tests over the interval exp(+-10000), the logarithms
5749393c00Smartynas  * of the random arguments were uniformly distributed over
5849393c00Smartynas  * [-10000, +10000].
5949393c00Smartynas  *
6049393c00Smartynas  * ERROR MESSAGES:
6149393c00Smartynas  *
6249393c00Smartynas  * log singularity:  x = 0; returns -INFINITY
6349393c00Smartynas  * log domain:       x < 0; returns NAN
6449393c00Smartynas  */
6549393c00Smartynas 
6649393c00Smartynas #include <math.h>
6749393c00Smartynas 
6812129755Smartynas #include "math_private.h"
6912129755Smartynas 
7049393c00Smartynas /* Coefficients for log(1+x) = x - x**2/2 + x**3 P(x)/Q(x)
7149393c00Smartynas  * 1/sqrt(2) <= x < sqrt(2)
7249393c00Smartynas  * Theoretical peak relative error = 2.32e-20
7349393c00Smartynas  */
7449393c00Smartynas static long double P[] = {
7549393c00Smartynas  4.5270000862445199635215E-5L,
7649393c00Smartynas  4.9854102823193375972212E-1L,
7749393c00Smartynas  6.5787325942061044846969E0L,
7849393c00Smartynas  2.9911919328553073277375E1L,
7949393c00Smartynas  6.0949667980987787057556E1L,
8049393c00Smartynas  5.7112963590585538103336E1L,
8149393c00Smartynas  2.0039553499201281259648E1L,
8249393c00Smartynas };
8349393c00Smartynas static long double Q[] = {
8449393c00Smartynas /* 1.0000000000000000000000E0,*/
8549393c00Smartynas  1.5062909083469192043167E1L,
8649393c00Smartynas  8.3047565967967209469434E1L,
8749393c00Smartynas  2.2176239823732856465394E2L,
8849393c00Smartynas  3.0909872225312059774938E2L,
8949393c00Smartynas  2.1642788614495947685003E2L,
9049393c00Smartynas  6.0118660497603843919306E1L,
9149393c00Smartynas };
9249393c00Smartynas 
9349393c00Smartynas /* Coefficients for log(x) = z + z^3 P(z^2)/Q(z^2),
9449393c00Smartynas  * where z = 2(x-1)/(x+1)
9549393c00Smartynas  * 1/sqrt(2) <= x < sqrt(2)
9649393c00Smartynas  * Theoretical peak relative error = 6.16e-22
9749393c00Smartynas  */
9849393c00Smartynas 
9949393c00Smartynas static long double R[4] = {
10049393c00Smartynas  1.9757429581415468984296E-3L,
10149393c00Smartynas -7.1990767473014147232598E-1L,
10249393c00Smartynas  1.0777257190312272158094E1L,
10349393c00Smartynas -3.5717684488096787370998E1L,
10449393c00Smartynas };
10549393c00Smartynas static long double S[4] = {
10649393c00Smartynas /* 1.00000000000000000000E0L,*/
10749393c00Smartynas -2.6201045551331104417768E1L,
10849393c00Smartynas  1.9361891836232102174846E2L,
10949393c00Smartynas -4.2861221385716144629696E2L,
11049393c00Smartynas };
1119fc13282Smartynas static const long double C1 = 6.9314575195312500000000E-1L;
1129fc13282Smartynas static const long double C2 = 1.4286068203094172321215E-6L;
11349393c00Smartynas 
11449393c00Smartynas #define SQRTH 0.70710678118654752440L
11549393c00Smartynas 
11649393c00Smartynas long double
logl(long double x)11749393c00Smartynas logl(long double x)
11849393c00Smartynas {
11949393c00Smartynas long double y, z;
12049393c00Smartynas int e;
12149393c00Smartynas 
12249393c00Smartynas if( isnan(x) )
12349393c00Smartynas 	return(x);
12449393c00Smartynas if( x == INFINITY )
12549393c00Smartynas 	return(x);
12649393c00Smartynas /* Test for domain */
12749393c00Smartynas if( x <= 0.0L )
12849393c00Smartynas 	{
12949393c00Smartynas 	if( x == 0.0L )
13049393c00Smartynas 		return( -INFINITY );
13149393c00Smartynas 	else
13249393c00Smartynas 		return( NAN );
13349393c00Smartynas 	}
13449393c00Smartynas 
13549393c00Smartynas /* separate mantissa from exponent */
13649393c00Smartynas 
13749393c00Smartynas /* Note, frexp is used so that denormal numbers
13849393c00Smartynas  * will be handled properly.
13949393c00Smartynas  */
14049393c00Smartynas x = frexpl( x, &e );
14149393c00Smartynas 
14249393c00Smartynas /* logarithm using log(x) = z + z**3 P(z)/Q(z),
14349393c00Smartynas  * where z = 2(x-1)/x+1)
14449393c00Smartynas  */
14549393c00Smartynas if( (e > 2) || (e < -2) )
14649393c00Smartynas {
14749393c00Smartynas if( x < SQRTH )
14849393c00Smartynas 	{ /* 2( 2x-1 )/( 2x+1 ) */
14949393c00Smartynas 	e -= 1;
15049393c00Smartynas 	z = x - 0.5L;
15149393c00Smartynas 	y = 0.5L * z + 0.5L;
15249393c00Smartynas 	}
15349393c00Smartynas else
15449393c00Smartynas 	{ /*  2 (x-1)/(x+1)   */
15549393c00Smartynas 	z = x - 0.5L;
15649393c00Smartynas 	z -= 0.5L;
15749393c00Smartynas 	y = 0.5L * x  + 0.5L;
15849393c00Smartynas 	}
15949393c00Smartynas x = z / y;
16049393c00Smartynas z = x*x;
16149393c00Smartynas z = x * ( z * __polevll( z, R, 3 ) / __p1evll( z, S, 3 ) );
16249393c00Smartynas z = z + e * C2;
16349393c00Smartynas z = z + x;
16449393c00Smartynas z = z + e * C1;
16549393c00Smartynas return( z );
16649393c00Smartynas }
16749393c00Smartynas 
16849393c00Smartynas 
16949393c00Smartynas /* logarithm using log(1+x) = x - .5x**2 + x**3 P(x)/Q(x) */
17049393c00Smartynas 
17149393c00Smartynas if( x < SQRTH )
17249393c00Smartynas 	{
17349393c00Smartynas 	e -= 1;
17449393c00Smartynas 	x = ldexpl( x, 1 ) - 1.0L; /*  2x - 1  */
17549393c00Smartynas 	}
17649393c00Smartynas else
17749393c00Smartynas 	{
17849393c00Smartynas 	x = x - 1.0L;
17949393c00Smartynas 	}
18049393c00Smartynas z = x*x;
18149393c00Smartynas y = x * ( z * __polevll( x, P, 6 ) / __p1evll( x, Q, 6 ) );
18249393c00Smartynas y = y + e * C2;
18349393c00Smartynas z = y - ldexpl( z, -1 );   /*  y - 0.5 * z  */
18449393c00Smartynas /* Note, the sum of above terms does not exceed x/4,
18549393c00Smartynas  * so it contributes at most about 1/4 lsb to the error.
18649393c00Smartynas  */
18749393c00Smartynas z = z + x;
18849393c00Smartynas z = z + e * C1; /* This sum has an error of 1/2 lsb. */
18949393c00Smartynas return( z );
19049393c00Smartynas }
1912f2c0062Sguenther DEF_STD(logl);
192