# Numerically stable computation of hyperbolic sine for $x \approx 0$

We are given numerically stable functions exp(x) and expm1(x) computing $$e^x$$ and $$e^{-x}$$. How compute numerically stably $$\sinh(x)=\frac{e^x-e^{-x}}{2}$$ for $$x \approx 0$$ using exp(x) and expm1(x)?

• Try $\sinh(x) = e^{-x}/2(e^{2x}-1).$ – Somos Nov 15 '19 at 1:44
• @Somos How to use the fact that $x \approx 0$? – adbeno Nov 15 '19 at 3:47
• @adbeno $(\exp(x) - \exp(-x)) / 2$ suffers from subtractive cancellation for $x \approx 0$. A common approach to fixing this issue is to eliminate the affected subtraction or addition by rewriting the formula as a product or quotient. This approach is exemplified by Somos's suggestion. – njuffa Nov 15 '19 at 8:14
• @adbeno The point is that $\text{expm1}(x):=e^x-1$ is computed in a stable way. – Somos Nov 15 '19 at 16:05
• You could also insert a zero and compute 0.5*(expm1(x)-expm1(-x)), but use the variant of @Somos if the evaluation of expm1 is much slower than exp. – Lutz Lehmann Nov 16 '19 at 9:29