4
$\begingroup$

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|$.

$\endgroup$
3
  • $\begingroup$ Closely related: math.stackexchange.com/questions/61209/…. "not appropriate for a big |x|" - it's solvable by computing it as $e^{\lfloor x \rfloor}\cdot e^{x - \lfloor x \rfloor}$. $\endgroup$
    – user114966
    Commented Mar 18, 2021 at 23:08
  • $\begingroup$ Also related: cs.stackexchange.com/questions/27832/… $\endgroup$
    – Pseudonym
    Commented Mar 19, 2021 at 4:16
  • 1
    $\begingroup$ Also perhaps an obvious point: Matlab may use its own proprietary algorithm. It's probably based on a standard one, though. $\endgroup$
    – Pseudonym
    Commented Mar 19, 2021 at 4:44

1 Answer 1

4
$\begingroup$

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} < e^x$ with eps > 0.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.