1

I want to evaluate $$f(x) = x \frac{\exp(ax)+\exp(-ax)}{\exp(ax)-\exp(-ax)}$$

This is a function approximate absolute value of $x$. However,as $a$ goes to large, the better approximation it gets. But this leads to numerical overflow as it will be an exponential function of something big

Any trick to avoid this ?

EditPiAf
  • 20,898
ElleryL
  • 1,583
  • 2
    For large positive $ax$ you can rewrite the ratio as $\frac{1+\exp(-2ax)}{1-\exp(-2ax)}$. Similar for large negative $ax$. Alternatively, you can try just evaluating $\tanh(ax)$ in another way entirely, for example by using its series at $\pm \infty$. – Ian Aug 28 '18 at 00:08
  • For positive values of $x$, divide numerator and denominator by $exp(ax)$. For negative $x$ multiply both numerator and denominator by $exp(ax)$. – M. Wind Aug 28 '18 at 00:09
  • Thanks for the help – ElleryL Aug 28 '18 at 00:14
  • Try $\sqrt{x^2+\epsilon^2}-\epsilon$. – Christian Blatter Aug 28 '18 at 09:38

0 Answers0