I want to calculate the error of exp function under finite precision(data type is double). Is taylor series or other special algorithm?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Generally, the best way to implement ex is by calling the
exp
function provided by your computing platform.Failing this, implementing the
exp
function is complicated and requires several esoteric skills. An implementation typically involves:Additionally:
Taylor series are inappropriate for evaluating functions because they are inaccurate away from their center points. This means they take too many terms to converge to the necessary precision. Having too many terms not only takes time but also makes it difficult to do the arithmetic accurately.
While I agree with much of what Eric said in his response, there are a few points worth adding. I wrote a tool that, among other things, allows evaluation of the exponential to high (user specified) precision.
When the precision can vary, one MUST resort to tools like series approximations, since then no single polynomial approximation will suffice. Even so, there are many tricks one can employ. In fact, I was surprised by the variety of tricks I used to accelerate such a series.
A good starter reference for any such endeavor is that by Hart, "Computer Approximations", a book that is sadly not easy to find. It offers many polynomial approximations, but also talks in moderate detail about range reduction tricks.
The exponential series is itself especially interesting. I describe the methods I employed for the exponential in section 4.1 of the file HPFMod2.pdf, which is included with HPF.
As an example, to compute exp(123.456789), one might try to use a series directly, but better is to store the value of e itself. Then we can use a range reduction to compute
We get the exponentials of powers of 2 by repeated squaring, then the fractional part will converge moderately rapidly. (E.g., 31 terms are needed for 100 digits of precision in the final series.)
However, suppose we just happened to notice that
Now, we could write the desired exponential as:
As long as we know (having stored it) the value of ln(10), the final series will require only about 17 terms to achieve a desired 100 digits of precision.