Python math.tanh() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.tanh(x) computes the hyperbolic tangent (sinh(x) / cosh(x)), smoothly mapping any real number into the interval [-1.0, 1.0].

Because both numerator and denominator grow at the same rate for huge |x|, math.tanh() never raises OverflowErrorβ€”it saturates cleanly at 1.0 and -1.0.

math.tanh() is a function in Python's standard math module: import it with import math and call math.tanh(...), or import the name directly with from math import tanh.

Syntax

Import math and pass any real number.

python
import math

y = math.tanh(x)
CallWhat it does
math.tanh(0)Returns 0.0.
math.tanh(1)Returns approximately 0.761594....
math.tanh(100)Returns 1.0 (saturates without OverflowError).
math.tanh(float("-inf"))Returns -1.0.

Its inverse on (-1, 1) is math.atanh().

Function signatures
python
math.tanh(x, /)
# returns: float hyperbolic tangent in [-1.0, 1.0]

Parameters

ParameterDescription
xRequired positional real number (int or float), including inf and -inf.

Return Value

Return ValueDescription
floatValue in [-1.0, 1.0]; returns 1.0 for inf, -1.0 for -inf, and nan for nan.

Python Examples

Example 1 — Squash Scores Into (-1, 1) With math.tanh()

Apply symmetric S-curve scaling across negative, zero, and positive inputs.

python
import math

for z in [-3.0, -1.0, 0.0, 1.0, 3.0]:
    val = math.tanh(z)
    print(f"z={z:+4.1f} -> tanh={val:+7.4f}")

How It Works

  1. Zero-centered output. Unlike sigmoid which runs from 0 to 1, math.tanh() centers cleanly at 0.0000.
  2. Odd symmetry. Opposing inputs -1.0 and +1.0 map to equal magnitudes -0.7616 and +0.7616.
  3. Smooth saturation. By z = 3.0, the curve is already over 99.5% of the way to one.

Example 2 — Model Skydiver Speed Approaching Terminal Velocity

Evaluate quadratic drag speed v_term * math.tanh(g * t / v_term) over drop time.

python
import math

v_term = 50.0
g = 10.0
for t in [0, 2, 5, 10, 30]:
    speed = v_term * math.tanh(g * t / v_term)
    print(f"t={t:>2}s -> speed={speed:>5.2f} m/s")

How It Works

  1. Linear start near zero. For small t, tanh(x) tracks x closely, matching free acceleration.
  2. Drag slows acceleration. By 10 seconds, speed reaches 48.20 m/s and eases toward the asymptote.
  3. Asymptotic cap. At 30 seconds, speed settles to terminal velocity 50.00 m/s.

Example 3 — Verify Saturation at Large Values and Infinities

Check that math.tanh() handles 1000, inf, and -inf without overflowing.

python
import math

print(math.tanh(1000), math.tanh(-1000))
print(math.tanh(float("inf")), math.tanh(float("-inf")))
print(math.copysign(1.0, math.tanh(-0.0)))

How It Works

  1. No overflow at 1000. Whereas sinh(1000) raises OverflowError, tanh(1000) returns 1.0.
  2. Infinite limits match. Passing inf and -inf yields exact 1.0 and -1.0.
  3. Signed zero preserved. By IEEE 754 convention, tanh(-0.0) remains -0.0.

Frequently Asked Questions

No. For large positive or negative inputs it saturates smoothly to 1.0 or -1.0 without overflowing.
Strictly between -1.0 and 1.0 mathematically, though float rounding produces exact 1.0 or -1.0 once |x| >= 19.07.
1.0 (and -1.0 for -inf).
Use math.atanh(y) for inputs strictly between -1 and 1.
Because it maps arbitrary scores smoothly into [-1, 1] while keeping zero mapped to zero.

Next: math.trunc()

Truncate x toward zero to an integer.

trunc() →

About the author

Mari Selvan M P
Mari Selvan M P πŸ”—

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful