Home Python math Functions tan() Python math.tan() Function 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.
import math
slope = math.tan(angle_radians) Call What 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 math.tan(x, /)
# returns: float tangent of x radians Parameters Parameter Description xRequired positional finite angle in radians (int or float).
Return Value Return Value Description floatTangent of x. Returns nan for nan.
Python Examples 1. Tower height 2. Road slope grade 3. Asymptote and inf Example 1 — Measure Tower Height From Ground Distance and Elevation Multiply baseline distance by math.tan(math.radians(angle)) for a surveyor sighting.
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") angle=15 deg -> height=13.40 m
angle=30 deg -> height=28.87 m
angle=45 deg -> height=50.00 m
angle=60 deg -> height=86.60 m How It Works Opposite over adjacent. Multiplying horizontal baseline by tangent gives vertical elevation directly.Forty-five degree identity. At 45 degrees tangent equals one, so height equals the 50.00 m distance.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.
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") 2.0 deg -> 3.49% grade
5.0 deg -> 8.75% grade
10.0 deg -> 17.63% grade
45.0 deg -> 100.00% grade How It Works Rise per hundred run. Highway grade percentage equals 100 * tan(angle).Small-angle proximity. At gentle slopes like two degrees, grade percentage grows close to linearly.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.
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") 0.9999999999999999
True
True
tan(inf) raises ValueError How It Works Just shy of one. Float rounding in pi / 4 leaves tan one ULP below 1.0.Huge at pi/2. Because math.pi / 2 does not hit the exact vertical pole, tangent spikes past 1e15 without dividing by zero.Infinity rejected. Passing float("inf") raises ValueError.Frequently Asked Questions Why does math.tan(math.pi / 2) return a huge number instead of raising an error? 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).
Does math.tan() take degrees? No. Pass math.radians(deg) to compute tangent of an angle in degrees.
Why does math.tan(math.pi / 4) return 0.9999999999999999? Floating-point rounding in math.pi / 4 produces a value one ULP away from exact pi/4. Use math.isclose(..., 1.0) or round().
What is the inverse of math.tan()? 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 does math.tan() raise ValueError? When given float("inf") or float("-inf").
Next: math.tanh() Return the hyperbolic tangent of x as a float.
tanh() → About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion