1 /* $OpenBSD: s_expm1l.c,v 1.2 2011/07/20 21:02:51 martynas Exp $ */
2
3 /*
4 * Copyright (c) 2008 Stephen L. Moshier <steve@moshier.net>
5 *
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.
9 *
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.
17 */
18
19 /* expm1l.c
20 *
21 * Exponential function, minus 1
22 * Long double precision
23 *
24 *
25 * SYNOPSIS:
26 *
27 * long double x, y, expm1l();
28 *
29 * y = expm1l( x );
30 *
31 *
32 *
33 * DESCRIPTION:
34 *
35 * Returns e (2.71828...) raised to the x power, minus 1.
36 *
37 * Range reduction is accomplished by separating the argument
38 * into an integer k and fraction f such that
39 *
40 * x k f
41 * e = 2 e.
42 *
43 * An expansion x + .5 x^2 + x^3 R(x) approximates exp(f) - 1
44 * in the basic range [-0.5 ln 2, 0.5 ln 2].
45 *
46 *
47 * ACCURACY:
48 *
49 * Relative error:
50 * arithmetic domain # trials peak rms
51 * IEEE -45,+MAXLOG 200,000 1.2e-19 2.5e-20
52 *
53 * ERROR MESSAGES:
54 *
55 * message condition value returned
56 * expm1l overflow x > MAXLOG MAXNUM
57 *
58 */
59
60
61 static const long double MAXLOGL = 1.1356523406294143949492E4L;
62
63 /* exp(x) - 1 = x + 0.5 x^2 + x^3 P(x)/Q(x)
64 -.5 ln 2 < x < .5 ln 2
65 Theoretical peak relative error = 3.4e-22 */
66
67 static const long double
68 P0 = -1.586135578666346600772998894928250240826E4L,
69 P1 = 2.642771505685952966904660652518429479531E3L,
70 P2 = -3.423199068835684263987132888286791620673E2L,
71 P3 = 1.800826371455042224581246202420972737840E1L,
72 P4 = -5.238523121205561042771939008061958820811E-1L,
73
74 Q0 = -9.516813471998079611319047060563358064497E4L,
75 Q1 = 3.964866271411091674556850458227710004570E4L,
76 Q2 = -7.207678383830091850230366618190187434796E3L,
77 Q3 = 7.206038318724600171970199625081491823079E2L,
78 Q4 = -4.002027679107076077238836622982900945173E1L,
79 /* Q5 = 1.000000000000000000000000000000000000000E0 */
80
81 /* C1 + C2 = ln 2 */
82 C1 = 6.93145751953125E-1L,
83 C2 = 1.428606820309417232121458176568075500134E-6L,
84 /* ln 2^-65 */
85 minarg = -4.5054566736396445112120088E1L;
86
87 long double
expm1l(long double x)88 expm1l(long double x)
89 {
90 long double px, qx, xx;
91 int k;
92
93 if( isnan(x) )
94 return(x + x);
95
96 /* Overflow. */
97 if (x > MAXLOGL) {
98 if (isinf(x))
99 return x;
100 return __math_oflowl(0);
101 }
102
103 if (x == 0.0l)
104 return x;
105
106 /* Minimum value. */
107 if (x < minarg) {
108 if (isinf(x))
109 return -1.0L;
110 return __LDBL_DENORM_MIN__ - 1.0L;
111 }
112
113 xx = C1 + C2;
114
115 /* Express x = ln 2 (k + remainder), remainder not exceeding 1/2. */
116 px = floorl (0.5l + x / xx);
117 k = px;
118 /* remainder times ln 2 */
119 x -= px * C1;
120 x -= px * C2;
121
122 /* Approximate exp(remainder ln 2). */
123 px = (((( P4 * x
124 + P3) * x
125 + P2) * x
126 + P1) * x
127 + P0) * x;
128
129 qx = (((( x
130 + Q4) * x
131 + Q3) * x
132 + Q2) * x
133 + Q1) * x
134 + Q0;
135
136 xx = x * x;
137 qx = x + (0.5l * xx + xx * px / qx);
138
139 /* exp(x) = exp(k ln 2) exp(remainder ln 2) = 2^k exp(remainder ln 2).
140 We have qx = exp(remainder ln 2) - 1, so
141 exp(x) - 1 = 2^k (qx + 1) - 1 = 2^k qx + 2^k - 1. */
142 if (k == __LDBL_MAX_EXP__)
143 px = __LDBL_MAX__;
144 else
145 px = ldexpl (1.0L, k);
146 x = px - 1.0L;
147 if (!isinfl(x))
148 x += px * qx;
149 return x;
150 }
151