HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

what algorithm is used to calculate the exponential function $e^x$?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
thewhatusedexponentialfunctionalgorithmcalculate

Problem

I am using Matlab daily. But I just realized that I even do not know what algorithm Matlab is using for calculating the exponential function $e^x$.

The power series expansion is obviously not appropriate for a big $|x|$.

Solution

Let $x = n \log_e(2) + x'$, then $e^x = 2^n \cdot e^{x'}$.
You can pick n so that $- 0.5\cdot \log_e(2) ≤ x' ≤ +0.5 \cdot \log_e(2)$.

$e^{x'}$ is easy to calculate because $x'$ is small, and $2^n$ can be calculated easiest by producing the bit represenation of $2^n$ as a floating point number.

That's the principle. You'll want to take care that x' is calculated with the highest possible precision, and that $e^{x'}$ is also calculated with the highest precision. And you want to take care if x and x+eps use different values n, that $e^x$ and $e^{x+eps}$ fit together nicely; you wouldn't want a sitation where $e^{x+eps} 0.

Context

StackExchange Computer Science Q#136806, answer score: 4

Revisions (0)

No revisions yet.