How this instrument works
tanh(x) is built by dividing sinh(x) by cosh(x) — the same construction that turns sine and cosine into ordinary tangent — except the outcome behaves nothing alike. The identity cosh²(x) − sinh²(x) = 1 pins cosh(x) as always at least as large in size as sinh(x), whatever real number x happens to be, so their quotient can never reach 1 in magnitude no matter how far x travels. Ordinary tangent has no such guardrail and shoots to infinity at regular intervals; tanh instead settles onto a smooth ceiling and floor at ±1 that it approaches but never touches.
Rewritten over a common exponential, tanh(x) = (eˣ − e⁻ˣ) ⁄ (eˣ + e⁻ˣ), which simplifies to (e²ˣ − 1) ⁄ (e²ˣ + 1) once the top and bottom are multiplied through by eˣ — a form worth knowing because it needs only one exponential call instead of two. The function's own derivative reads back in terms of itself: d⁄dx tanh(x) = 1 − tanh²(x), so once a forward pass has produced tanh(x), the slope at that point falls out of subtraction and squaring alone, with no exponential left to recompute — the shortcut a neural network's backward pass leans on at every layer.
tanh is also a rescaled twin of the logistic sigmoid σ(x) = 1 ⁄ (1 + e⁻ˣ), the other S-curve neural networks lean on for gating: tanh(x) = 2σ(2x) − 1. Swap one activation function for the other inside a network and the shape does not change, only its vertical stretch and its resting height — sigmoid sits between 0 and 1 and starts there at x = 0, while tanh sits between −1 and 1 and starts at 0, which is why a zero-centered tanh is often preferred for hidden layers even though the underlying curve is the same one, seen at a different scale.
- Enter any real number into the x field — tanh takes positive figures, negative figures, and zero with no domain restriction at all.
- tanh(x) reports the result: setting x to 1 returns 0.7615941559557649, the exact quotient of sinh(1) over cosh(1).
- Switch the sign to x = −1: tanh(x) answers −0.7615941559557649 — the same size, flipped, because tanh is odd.
- Set x to 0 to see the single crossing point: tanh(x) reports exactly 0, the midpoint between its floor of −1 and ceiling of 1.
- Push x out to 5 and watch tanh(x) climb to 0.9999092042625951 — already a hair's breadth from its ceiling of 1.
Worked example — tanh(1) confirmed three ways
Feed x = 1 into the sheet and tanh(x) comes back as 0.7615941559557649. That figure is a quotient of two numbers computed along the way, sinh(1) = 1.1752011936438014 and cosh(1) = 1.5430806348152437, and 1.1752011936438014 ⁄ 1.5430806348152437 lands on 0.7615941559557649 to the full precision shown. The doubled-exponent form agrees independently: e² ≈ 7.38905609893065, so (e² − 1) ⁄ (e² + 1) = 6.38905609893065 ⁄ 8.38905609893065 = 0.7615941559557649, the same figure by an entirely separate route.
Run x = −1 through the same sheet and tanh(x) answers −0.7615941559557649, the exact negative of the first result, since tanh is an odd function and flipping the sign of x flips the sign of the quotient without touching its size. Try x = 0 instead and tanh(x) settles on exactly 0, the single point the curve crosses on its way from a floor of −1 to a ceiling of 1 — the resting value ordinary tangent shares at zero too, before the two functions part ways for every other input.
The sigmoid identity checks out on the same input: the logistic sigmoid at 2 is σ(2) = 1 ⁄ (1 + e⁻²) ≈ 0.880797, and 2 × 0.880797 − 1 = 0.761594, matching tanh(1) = 0.7615941559557649 to six decimal places. Pushed further out, x = 5 gives tanh(x) = 0.9999092042625951 — already within one part in ten thousand of the ceiling of 1, which is how quickly this function saturates once its input leaves the neighborhood of zero.
Questions
What is tanh(x), and how is it built from sinh and cosh?
tanh(x) is sinh(x) divided by cosh(x) — the hyperbolic counterpart to ordinary tangent's sine-over-cosine construction. Because cosh(x) is always at least as large as sinh(x) in absolute value — the identity cosh²(x) − sinh²(x) = 1 guarantees it — the quotient can never reach ±1, only approach those bounds as x grows large in either direction.
Why is tanh(x) always trapped between −1 and 1?
Because sinh(x) and cosh(x) satisfy cosh²(x) − sinh²(x) = 1 for every real x, cosh(x) is always the larger of the two in magnitude, so dividing sinh(x) by cosh(x) can never reach 1 in size. As x grows large, e⁻ˣ becomes negligible and both sinh(x) and cosh(x) collapse toward the same value eˣ⁄2, pulling their ratio toward exactly 1 — but never onto it.
How does tanh relate to the logistic sigmoid used in neural networks?
tanh(x) is a rescaled sigmoid: tanh(x) = 2σ(2x) − 1, where σ(x) = 1 ⁄ (1 + e⁻ˣ). Both trace the same S-shaped curve, but sigmoid runs from 0 to 1 while tanh runs from −1 to 1 and is centered on zero — which is why many networks prefer tanh in hidden layers, where a zero-centered output tends to make training converge faster.
What is the derivative of tanh(x), and why does it matter?
d⁄dx tanh(x) = 1 − tanh²(x) — the slope is written entirely in terms of the function's own output, with no exponential left to evaluate. That shortcut is exactly why tanh became a popular neural-network activation: once a layer's forward pass has produced tanh(x), the gradient needed for backpropagation costs only a subtraction and a squaring, not a fresh exponential call.
How is tanh(x) different from ordinary tangent, tan(x)?
They're built the same way — a sine-like function divided by a cosine-like one — but the results diverge completely. tan(x) repeats every π and shoots to infinity at odd multiples of π⁄2; tanh(x) never repeats and never blows up, settling smoothly onto a ceiling of 1 and a floor of −1 instead of a vertical asymptote.
What's the easiest mistake to make computing tanh by hand?
Forgetting the doubled exponent in the single-fraction form: tanh(x) = (e²ˣ − 1) ⁄ (e²ˣ + 1), not (eˣ − 1) ⁄ (eˣ + 1). The two look one keystroke apart but give different curves entirely — the correct form comes from multiplying (eˣ − e⁻ˣ) ⁄ (eˣ + e⁻ˣ) top and bottom by eˣ.