Python Glossary Русский

all Python 3Numbers & mathfunction

math.tanh

hyperbolic

The hyperbolic tangent of x: sinh(x) / cosh(x); values lie between -1 and 1.

Syntax

math.tanh(...)

This is the one hyperbolic function that cannot overflow: for any finite argument the result stays within [-1, 1]. It saturates fast, though — just past 19 (from about 19.1) the float is indistinguishable from one and you get exactly 1.0 back, so trying to invert it with math.atanh at that point raises ValueError.

Examples

import math
print(math.tanh(0))   # → 0.0
print(math.tanh(1))   # → 0.7615941559557649
print(round(math.tanh(-1), 4))   # → -0.7616
print(math.isclose(math.tanh(1), math.sinh(1) / math.cosh(1)))   # → True
print(math.tanh(1000))   # → 1.0

See also

Open in the full glossarydocs.python.orgReport an inaccuracy