# How to compute exp(x) for x in [0,ln(2)] in double precision with high accuracy, possibly at most 0.5 ulp error?

How to compute exp(x) with correct rounding?
How to compute exp(x) for x∈[0,ln(2)] in double precision with high accuracy, possibly at most 0.5 ulp error?
It is possible to obtain low error. After testing a few libraries I have found one with 1 ulp accuracy. However I am not able to find any reference, which delivers precise error bounds for computed value.
Calculating exp(x) is not a trivial task. Althougth is is easy to obtain fast converging polynomial approximating this function, for example the Taylor series, roundoff errors occuring during evaluation of a polynomial are quite high. For example if we want to evaluate a polynomial p(x)=∑N−1i=0aixi, then apriori relative forward error is bounded as
$|\frac{\mathrm{fl}\left(p\left(x\right)\right)-p\left(x\right)}{p\left(x\right)}|\le \mathrm{cond}\left(p,x\right)×u+O\left({u}^{2}\right)$
where
$\mathrm{cond}\left(p,x\right)=\frac{2N}{1-2Nu}×|\frac{\sum _{i=0}^{N}|{a}_{i}||x{|}^{i}}{p\left(x\right)}|\ge 2N$
and u is the unit roundoff (half of the machine precision).
Therefore even if we have a polynomial approximation of exp(x) of order 5 (which is hard by itself), with truncation error at most 0.5 ulp, then still roundoff error is at least 5. However 1 ulp error is possible.
My question is what kind of tricks can be used here to obtain high accuracy (without using high precision arithmetics). Is it even possible to obtain 0.5 ulp error?
You can still ask an expert for help

## Want to know more about Polynomial arithmetic?

• Questions are typically answered in as fast as 30 minutes

Solve your problem for the price of one coffee

• Math expert for every subject
• Pay only if we can solve it

Messiah Trevino
Step 1
I have found one nice formula for the exp function:
$\begin{array}{}\text{(1)}& G\left(x\right)& =\frac{\mathrm{coth}\left(\sqrt{x}/2\right)}{\sqrt{x}}-\frac{2}{x}\text{(2)}& c& =x-G\left({x}^{2}\right)×{x}^{2}\text{(3)}& \mathrm{exp}\left(x\right)& =1+\left(\frac{xc}{2-c}+x\right)\end{array}$
This formula was used for interval [-ln(2)/2,ln(2)/2] but this is also good (transformation from this interval to [0,ln(2)] can be done without any errors. Unfortunately I don't have any reference on this scheme. I found this scheme using "reverse engeneering" of a library source.
The function G(x) is nearly linear in this interval. This function can be accurately approximated by a polynomial of order 4 using the Remez algorithm. Its Taylor expansion is
$G\left(x\right)\approx \frac{1}{6}-\frac{x}{360}+\frac{{x}^{2}}{15120}-\frac{{x}^{3}}{604800}+\frac{{x}^{4}}{23950080}$
Step 2
Condition number of this polynomial in given interval is very good, $1.012×{\gamma }_{5}$, ${\gamma }_{N}=2N/\left(1-2Nu\right)$.
Expression (2) cannot be computed accurately. However (3) suppresses most of numerical errors. For example condition number of (3) with respect to c is 0.0619, thus error if computing c, ${\delta }_{c}$, contributes to the total error as $0.0619×{\delta }_{c}$, similarly $xc/\left(2-c\right)$ is inaccurate, but error in computing this subexpression ${\delta }_{xc}$ contributes to error of $xc/\left(2-c\right)+x$ approximately as $0.2×{\delta }_{xc}$, which again is suppressed in the final sum (3) (approximately twice).
Final error of computing exp by (1)-(3) is less than 1 (maximum error found is 0.83).
It is possible to compute exp(x) even with 0.5 ulp error using the compensated Horner's scheme. See "Faithful Polynomial Evaluation with Compensated Horner Algorithm", P. Langlois, N. Louvet, 2006, for details. This scheme must however be corrected in order to take into account errors in computed coefficients of approximation polynomial. As the polynomial approximation of exp(x) we can use the Taylor expansion of order 27. Probably we can use lower order approximation, in tests I was not able to find a case with higher than 0.5 ulp even in case of approximation of order 20. Moreover this scheme is quite fast, at most 8-times slower, than library implementation of exp.