1 |
|
|
/* $OpenBSD: e_expl.c,v 1.4 2016/09/12 19:47:02 guenther 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 |
|
|
/* expl.c |
20 |
|
|
* |
21 |
|
|
* Exponential function, long double precision |
22 |
|
|
* |
23 |
|
|
* |
24 |
|
|
* |
25 |
|
|
* SYNOPSIS: |
26 |
|
|
* |
27 |
|
|
* long double x, y, expl(); |
28 |
|
|
* |
29 |
|
|
* y = expl( x ); |
30 |
|
|
* |
31 |
|
|
* |
32 |
|
|
* |
33 |
|
|
* DESCRIPTION: |
34 |
|
|
* |
35 |
|
|
* Returns e (2.71828...) raised to the x power. |
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 |
|
|
* A Pade' form of degree 2/3 is used to approximate 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 +-10000 50000 1.12e-19 2.81e-20 |
52 |
|
|
* |
53 |
|
|
* |
54 |
|
|
* Error amplification in the exponential function can be |
55 |
|
|
* a serious matter. The error propagation involves |
56 |
|
|
* exp( X(1+delta) ) = exp(X) ( 1 + X*delta + ... ), |
57 |
|
|
* which shows that a 1 lsb error in representing X produces |
58 |
|
|
* a relative error of X times 1 lsb in the function. |
59 |
|
|
* While the routine gives an accurate result for arguments |
60 |
|
|
* that are exactly represented by a long double precision |
61 |
|
|
* computer number, the result contains amplified roundoff |
62 |
|
|
* error for large arguments not exactly represented. |
63 |
|
|
* |
64 |
|
|
* |
65 |
|
|
* ERROR MESSAGES: |
66 |
|
|
* |
67 |
|
|
* message condition value returned |
68 |
|
|
* exp underflow x < MINLOG 0.0 |
69 |
|
|
* exp overflow x > MAXLOG MAXNUM |
70 |
|
|
* |
71 |
|
|
*/ |
72 |
|
|
|
73 |
|
|
/* Exponential function */ |
74 |
|
|
|
75 |
|
|
#include <math.h> |
76 |
|
|
|
77 |
|
|
#include "math_private.h" |
78 |
|
|
|
79 |
|
|
static long double P[3] = { |
80 |
|
|
1.2617719307481059087798E-4L, |
81 |
|
|
3.0299440770744196129956E-2L, |
82 |
|
|
9.9999999999999999991025E-1L, |
83 |
|
|
}; |
84 |
|
|
static long double Q[4] = { |
85 |
|
|
3.0019850513866445504159E-6L, |
86 |
|
|
2.5244834034968410419224E-3L, |
87 |
|
|
2.2726554820815502876593E-1L, |
88 |
|
|
2.0000000000000000000897E0L, |
89 |
|
|
}; |
90 |
|
|
static const long double C1 = 6.9314575195312500000000E-1L; |
91 |
|
|
static const long double C2 = 1.4286068203094172321215E-6L; |
92 |
|
|
static const long double MAXLOGL = 1.1356523406294143949492E4L; |
93 |
|
|
static const long double MINLOGL = -1.13994985314888605586758E4L; |
94 |
|
|
static const long double LOG2EL = 1.4426950408889634073599E0L; |
95 |
|
|
|
96 |
|
|
long double |
97 |
|
|
expl(long double x) |
98 |
|
|
{ |
99 |
|
|
long double px, xx; |
100 |
|
|
int n; |
101 |
|
|
|
102 |
✗✓ |
20 |
if( isnan(x) ) |
103 |
|
|
return(x); |
104 |
✓✓ |
10 |
if( x > MAXLOGL) |
105 |
|
5 |
return( INFINITY ); |
106 |
|
|
|
107 |
✓✗ |
5 |
if( x < MINLOGL ) |
108 |
|
5 |
return(0.0L); |
109 |
|
|
|
110 |
|
|
/* Express e**x = e**g 2**n |
111 |
|
|
* = e**g e**( n loge(2) ) |
112 |
|
|
* = e**( g + n loge(2) ) |
113 |
|
|
*/ |
114 |
|
|
px = floorl( LOG2EL * x + 0.5L ); /* floor() truncates toward -infinity. */ |
115 |
|
|
n = px; |
116 |
|
|
x -= px * C1; |
117 |
|
|
x -= px * C2; |
118 |
|
|
|
119 |
|
|
|
120 |
|
|
/* rational approximation for exponential |
121 |
|
|
* of the fractional part: |
122 |
|
|
* e**x = 1 + 2x P(x**2)/( Q(x**2) - P(x**2) ) |
123 |
|
|
*/ |
124 |
|
|
xx = x * x; |
125 |
|
|
px = x * __polevll( xx, P, 2 ); |
126 |
|
|
x = px/( __polevll( xx, Q, 3 ) - px ); |
127 |
|
|
x = 1.0L + ldexpl( x, 1 ); |
128 |
|
|
|
129 |
|
|
x = ldexpl( x, n ); |
130 |
|
|
return(x); |
131 |
|
10 |
} |
132 |
|
|
DEF_STD(expl); |