1 /* origin: OpenBSD /usr/src/lib/libm/src/ld80/e_expl.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 * Exponential function, long double precision
23 * long double x, y, expl();
30 * Returns e (2.71828...) raised to the x power.
32 * Range reduction is accomplished by separating the argument
33 * into an integer k and fraction f such that
38 * A Pade' form of degree 2/3 is used to approximate exp(f) - 1
39 * in the basic range [-0.5 ln 2, 0.5 ln 2].
45 * arithmetic domain # trials peak rms
46 * IEEE +-10000 50000 1.12e-19 2.81e-20
49 * Error amplification in the exponential function can be
50 * a serious matter. The error propagation involves
51 * exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ),
52 * which shows that a 1 lsb error in representing X produces
53 * a relative error of X times 1 lsb in the function.
54 * While the routine gives an accurate result for arguments
55 * that are exactly represented by a long double precision
56 * computer number, the result contains amplified roundoff
57 * error for large arguments not exactly represented.
62 * message condition value returned
63 * exp underflow x < MINLOG 0.0
64 * exp overflow x > MAXLOG MAXNUM
70 #if LDBL_MANT_DIG == 53 && LDBL_MAX_EXP == 1024
71 long double expl(long double x)
75 #elif LDBL_MANT_DIG == 64 && LDBL_MAX_EXP == 16384
77 static long double P[3] = {
78 1.2617719307481059087798E-4L,
79 3.0299440770744196129956E-2L,
80 9.9999999999999999991025E-1L,
82 static long double Q[4] = {
83 3.0019850513866445504159E-6L,
84 2.5244834034968410419224E-3L,
85 2.2726554820815502876593E-1L,
86 2.0000000000000000000897E0L,
88 static const long double
89 C1 = 6.9314575195312500000000E-1L,
90 C2 = 1.4286068203094172321215E-6L,
91 MAXLOGL = 1.1356523406294143949492E4L,
92 MINLOGL = -1.13994985314888605586758E4L,
93 LOG2EL = 1.4426950408889634073599E0L;
95 long double expl(long double x)
107 /* Express e**x = e**g 2**n
108 * = e**g e**(n loge(2))
109 * = e**(g + n loge(2))
111 px = floorl(LOG2EL * x + 0.5L); /* floor() truncates toward -infinity. */
116 /* rational approximation for exponential
117 * of the fractional part:
118 * e**x = 1 + 2x P(x**2)/(Q(x**2) - P(x**2))
121 px = x * __polevll(xx, P, 2);
122 x = px/(__polevll(xx, Q, 3) - px);
123 x = 1.0L + ldexpl(x, 1);