Exp(x) Log2

advertisement
Exp(x)
Log2
The use of log(2) is implicit in C Abramowitz and Stegun 4.2.43 (AEXP.for)
where the approximations to exp(x) all are for 0  x  log(2). In double precision define
k  x / log 2  .5
(1)
The multiple precision log(2) nrlog.doc .htm is needed for the subtractions step.
(2)
xm  x  k log 2
-log(2)/2  xm  log(2)/2
So that
exp  x   exp  k log 2   exp  xm 
(3)
 2k exp  xm 
exp(xm)
The algorithm is based on one by David M. Smith1. More details in ExpLn.doc.
The

exp  x   exp  x / 2
k

2k
(4)
k
Define c = x/2 and evaluate exp(c) using
(5)
exp(c)  1  c  c 2 / 2  c3 / 3!  c n / n!
The accuracy will be higher if the 1 is suppressed so that the actual expansion is
exp(c)  1  c  c 2 / 2  c3 / 3!  c n / n! (6)
Notice that this expansion is inappropriate for large negative values of c. The
value of the exponent is going towards zero, which requires enormous cancellation for
large negative c. The appropriate way to calculate the value for large negative c is to find
exp(-c) and then invert. The c’s needed range from –ln(2)/2 to + ln(2)/2.
The error in a multi-precsion number is discussed in MPDetail.doc. For c small the
exponential expands with a first term, IEXP(1) = 1. Thus the error in exp(c) that comes
from representing it by a finite sum of terms is
1
 MP  b1it (7)
2
The error that results from truncating (5) at the term n is less than the last term held
n
 Trunc  c / n !
(8)
Equating these
1
n
c / n !  b1it
(9)
2
Stirling’s approximation for n and substituting |x|/2k for |c| gives
n log x  nk log 2  n log n   log 2  1  it  log b (10)
1
See ExpLn.doc for detailed discussion. Efficient Multiple-Precision Evaluation of Elementary Functions,
by David M. Smith Mathematics of Computation 52 (1989) 131 – 134
The largest terms are middle one on the left and the last one on the right so that for large
k
nk   it  1 log b / log 2
In the present case b = 214, so that log(b)/log(2) = 14. There are k multiplications for the
k squarings and n multiplications for the expansion so that
Ops  n  k
(11)
 n  14  it  1 / n
Set the derivative with respect to n to zero to find
0  14  it  1 / n2  1
(12)
n  14  it  1
In this approximation, n is independent of x. The accuracy is kept the same for all x by
using (10) to solve for k.
l  it  1 log b / log 2
k
 log n / log 2  log x / log 2
(13)
n
The test code is tmpexps.for.
Download