I don't know much about computing functions in general but I would like to understand how Mathematica computes the hyperbolic tangent function for large values of x.
How do you compute the hyperbolic tangent function for high values of x?
Hyberpolic tangent function at mathworld
$\endgroup$ 31 Answer
$\begingroup$Disclaimer: I have no idea how Mathematica does it. Wolfram like to keep their algorithms quiet, or at least quieter than other mathematics software providers.
We may write $$ \tanh{x} = \frac{e^x-e^{-x}}{e^x+e^{-x}} = \frac{e^{2x}-1}{e^{2x}+1} = 1 - \frac{2e^{-2x}}{1+e^{-2x}}. $$ This then has an expansion in terms of an alternating series of decaying exponentials, which decrease very rapidly to zero: $$ 1 - \frac{2e^{-2x}}{1+e^{-2x}} = 1 + 2\sum_{k=1}^{\infty} (-1)^k(e^{-2x})^k $$ So calculate $e^{-2x}$ accurately and it's easy.
$\endgroup$ 4