1*2f2c0062Sguenther /* $OpenBSD: e_expl.c,v 1.4 2016/09/12 19:47:02 guenther 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 /* expl.c
2049393c00Smartynas *
2149393c00Smartynas * Exponential function, long double precision
2249393c00Smartynas *
2349393c00Smartynas *
2449393c00Smartynas *
2549393c00Smartynas * SYNOPSIS:
2649393c00Smartynas *
2749393c00Smartynas * long double x, y, expl();
2849393c00Smartynas *
2949393c00Smartynas * y = expl( x );
3049393c00Smartynas *
3149393c00Smartynas *
3249393c00Smartynas *
3349393c00Smartynas * DESCRIPTION:
3449393c00Smartynas *
3549393c00Smartynas * Returns e (2.71828...) raised to the x power.
3649393c00Smartynas *
3749393c00Smartynas * Range reduction is accomplished by separating the argument
3849393c00Smartynas * into an integer k and fraction f such that
3949393c00Smartynas *
4049393c00Smartynas * x k f
4149393c00Smartynas * e = 2 e.
4249393c00Smartynas *
4349393c00Smartynas * A Pade' form of degree 2/3 is used to approximate exp(f) - 1
4449393c00Smartynas * in the basic range [-0.5 ln 2, 0.5 ln 2].
4549393c00Smartynas *
4649393c00Smartynas *
4749393c00Smartynas * ACCURACY:
4849393c00Smartynas *
4949393c00Smartynas * Relative error:
5049393c00Smartynas * arithmetic domain # trials peak rms
5149393c00Smartynas * IEEE +-10000 50000 1.12e-19 2.81e-20
5249393c00Smartynas *
5349393c00Smartynas *
5449393c00Smartynas * Error amplification in the exponential function can be
5549393c00Smartynas * a serious matter. The error propagation involves
5649393c00Smartynas * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
5749393c00Smartynas * which shows that a 1 lsb error in representing X produces
5849393c00Smartynas * a relative error of X times 1 lsb in the function.
5949393c00Smartynas * While the routine gives an accurate result for arguments
6049393c00Smartynas * that are exactly represented by a long double precision
6149393c00Smartynas * computer number, the result contains amplified roundoff
6249393c00Smartynas * error for large arguments not exactly represented.
6349393c00Smartynas *
6449393c00Smartynas *
6549393c00Smartynas * ERROR MESSAGES:
6649393c00Smartynas *
6749393c00Smartynas * message condition value returned
6849393c00Smartynas * exp underflow x < MINLOG 0.0
6949393c00Smartynas * exp overflow x > MAXLOG MAXNUM
7049393c00Smartynas *
7149393c00Smartynas */
7249393c00Smartynas
7349393c00Smartynas /* Exponential function */
7449393c00Smartynas
7549393c00Smartynas #include <math.h>
7649393c00Smartynas
7712129755Smartynas #include "math_private.h"
7812129755Smartynas
7949393c00Smartynas static long double P[3] = {
8049393c00Smartynas 1.2617719307481059087798E-4L,
8149393c00Smartynas 3.0299440770744196129956E-2L,
8249393c00Smartynas 9.9999999999999999991025E-1L,
8349393c00Smartynas };
8449393c00Smartynas static long double Q[4] = {
8549393c00Smartynas 3.0019850513866445504159E-6L,
8649393c00Smartynas 2.5244834034968410419224E-3L,
8749393c00Smartynas 2.2726554820815502876593E-1L,
8849393c00Smartynas 2.0000000000000000000897E0L,
8949393c00Smartynas };
909fc13282Smartynas static const long double C1 = 6.9314575195312500000000E-1L;
919fc13282Smartynas static const long double C2 = 1.4286068203094172321215E-6L;
929fc13282Smartynas static const long double MAXLOGL = 1.1356523406294143949492E4L;
939fc13282Smartynas static const long double MINLOGL = -1.13994985314888605586758E4L;
949fc13282Smartynas static const long double LOG2EL = 1.4426950408889634073599E0L;
9549393c00Smartynas
9649393c00Smartynas long double
expl(long double x)9749393c00Smartynas expl(long double x)
9849393c00Smartynas {
9949393c00Smartynas long double px, xx;
10049393c00Smartynas int n;
10149393c00Smartynas
10249393c00Smartynas if( isnan(x) )
10349393c00Smartynas return(x);
10449393c00Smartynas if( x > MAXLOGL)
10549393c00Smartynas return( INFINITY );
10649393c00Smartynas
10749393c00Smartynas if( x < MINLOGL )
10849393c00Smartynas return(0.0L);
10949393c00Smartynas
11049393c00Smartynas /* Express e**x = e**g 2**n
11149393c00Smartynas * = e**g e**( n loge(2) )
11249393c00Smartynas * = e**( g + n loge(2) )
11349393c00Smartynas */
11449393c00Smartynas px = floorl( LOG2EL * x + 0.5L ); /* floor() truncates toward -infinity. */
11549393c00Smartynas n = px;
11649393c00Smartynas x -= px * C1;
11749393c00Smartynas x -= px * C2;
11849393c00Smartynas
11949393c00Smartynas
12049393c00Smartynas /* rational approximation for exponential
12149393c00Smartynas * of the fractional part:
12249393c00Smartynas * e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) )
12349393c00Smartynas */
12449393c00Smartynas xx = x * x;
12549393c00Smartynas px = x * __polevll( xx, P, 2 );
12649393c00Smartynas x = px/( __polevll( xx, Q, 3 ) - px );
12749393c00Smartynas x = 1.0L + ldexpl( x, 1 );
12849393c00Smartynas
12949393c00Smartynas x = ldexpl( x, n );
13049393c00Smartynas return(x);
13149393c00Smartynas }
132*2f2c0062Sguenther DEF_STD(expl);
133