Python math.tan() Function

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

Definition

math.tan(x) returns the trigonometric tangent (sin(x) / cos(x)) of angle x in radians.

Because math.pi / 2 is slightly below true pi/2, math.tan(math.pi / 2) does not raise ZeroDivisionError; instead it returns a huge finite float around 1.63e+16.

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

Syntax

Import math and pass an angle in radians.

python
import math

slope = math.tan(angle_radians)
CallWhat it does
math.tan(0)Returns 0.0.
math.tan(math.pi / 4)Returns 0.9999999999999999 (close to 1.0).
math.tan(-math.pi / 4)Returns -0.9999999999999999.
math.tan(float("inf"))Raises ValueError: domain error.

Use math.atan() or math.atan2() to turn a slope or (dy, dx) offset back into an angle.

Function signatures
python
math.tan(x, /)
# returns: float tangent of x radians

Parameters

ParameterDescription
xRequired positional finite angle in radians (int or float).

Return Value

Return ValueDescription
floatTangent of x. Returns nan for nan.

Python Examples

Example 1 — Measure Tower Height From Ground Distance and Elevation

Multiply baseline distance by math.tan(math.radians(angle)) for a surveyor sighting.

python
import math

baseline_m = 50.0
for angle_deg in [15, 30, 45, 60]:
    height = baseline_m * math.tan(math.radians(angle_deg))
    print(f"angle={angle_deg} deg -> height={height:.2f} m")

How It Works

  1. Opposite over adjacent. Multiplying horizontal baseline by tangent gives vertical elevation directly.
  2. Forty-five degree identity. At 45 degrees tangent equals one, so height equals the 50.00 m distance.
  3. Rapid climb past 45. Moving from 45 to 60 degrees nearly doubles the measured height.

Example 2 — Convert Highway Slope Angles Into Grade Percentages

Express steepness as 100 * math.tan(angle) for civil engineering gradients.

python
import math

for slope_deg in [2.0, 5.0, 10.0, 45.0]:
    grade_pct = 100.0 * math.tan(math.radians(slope_deg))
    print(f"{slope_deg:>4} deg -> {grade_pct:>6.2f}% grade")

How It Works

  1. Rise per hundred run. Highway grade percentage equals 100 * tan(angle).
  2. Small-angle proximity. At gentle slopes like two degrees, grade percentage grows close to linearly.
  3. Full 100% grade. A 45.0 degree slope rises one meter per horizontal meter, giving 100.00%.

Example 3 — Inspect 45 Degrees, Near-Asymptote Growth, and Infinity

Compare tan(pi / 4) with 1.0, test spike near pi / 2, and catch ValueError on inf.

python
import math

print(math.tan(math.pi / 4.0))
print(math.isclose(math.tan(math.pi / 4.0), 1.0))
print(abs(math.tan(math.pi / 2.0)) > 1e15)

try:
    math.tan(float("inf"))
except ValueError:
    print("tan(inf) raises ValueError")

How It Works

  1. Just shy of one. Float rounding in pi / 4 leaves tan one ULP below 1.0.
  2. Huge at pi/2. Because math.pi / 2 does not hit the exact vertical pole, tangent spikes past 1e15 without dividing by zero.
  3. Infinity rejected. Passing float("inf") raises ValueError.

Frequently Asked Questions

Because math.pi is slightly rounded, math.pi / 2 is just shy of exact pi/2, so cos(math.pi / 2) is ~6.12e-17 rather than 0, making tan huge (~1.63e16).
No. Pass math.radians(deg) to compute tangent of an angle in degrees.
Floating-point rounding in math.pi / 4 produces a value one ULP away from exact pi/4. Use math.isclose(..., 1.0) or round().
math.atan(slope) returns the angle in (-pi/2, pi/2). Use math.atan2(y, x) when you have both components and want full 360-degree quadrants.
When given float("inf") or float("-inf").

Next: math.tanh()

Return the hyperbolic tangent of x as a float.

tanh() →

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.

12 people found this page helpful