The math.atan(x) function computes the arc tangent (inverse tangent) of a numeric value x. Given any real slope ratio $x \in (-\infty, +\infty)$, it returns the corresponding principal angle in radians in the open interval $(-rac{\pi}{2}, rac{\pi}{2})$ (or $-90^\circ$ to $+90^\circ$).
Exact Inverse: math.atan() reverses math.tan() (math.tan(math.atan(x)) == x for any real number x).
The math.atan() function is part of the standard math module:
import math
angle_radians = math.atan(x)
| Parameter | Type | Required? | Description |
|---|---|---|---|
x |
float or int |
Yes | Any real number representing a slope or tangent ratio ($rac{ ext{rise}}{ ext{run}}$). The domain is completely unrestricted: $(-\infty, +\infty)$. |
Return Value: Returns a floating-point number (float) representing the angle in radians strictly bounded within $(-rac{\pi}{2}, rac{\pi}{2})$.
Evaluating arc tangent for positive, zero, and negative slope ratios:
import math
print("atan(0): ", math.atan(0)) # 0.0 radians
print("atan(1.0): ", math.atan(1.0)) # 0.7853981633974483 (~pi / 4, 45 degrees)
print("atan(-1.0):", math.atan(-1.0)) # -0.7853981633974483 (-45 degrees)
print("atan(10.0):", math.atan(10.0)) # 1.4711276743037347 radians (~84.29 degrees)
atan(0): 0.0
atan(1.0): 0.7853981633974483
atan(-1.0): -0.7853981633974483
atan(10.0): 1.4711276743037347
To convert the returned radian angle into human-readable degrees ($ -90^\circ $ to $ +90^\circ $), pass the result to math.degrees():
import math
ratios = [0.0, 0.57735, 1.0, 1.73205, 5.0]
for r in ratios:
rad = math.atan(r)
deg = math.degrees(rad)
print(f"atan({r:7.4f}) -> {rad:6.4f} rad == {deg:5.1f}°")
atan( 0.0000) -> 0.0000 rad == 0.0°
atan( 0.5774) -> 0.5236 rad == 30.0°
atan( 1.0000) -> 0.7854 rad == 45.0°
atan( 1.7321) -> 1.0472 rad == 60.0°
atan( 5.0000) -> 1.3734 rad == 78.7°
Unlike math.asin() and math.acos() which throw a ValueError for inputs outside $[-1.0, 1.0]$, math.atan() never raises a domain error. As the input ratio grows toward positive or negative infinity, the return value approaches horizontal asymptotes at $\pmrac{\pi}{2}$:
import math
print("atan(1,000,000): ", math.atan(1_000_000)) # 1.5707953267948965 (≈ pi / 2)
print("atan(-1,000,000):", math.atan(-1_000_000)) # -1.5707953267948965 (≈ -pi / 2)
print("pi / 2 reference: ", math.pi / 2) # 1.5707963267948966
When working with Cartesian 2D coordinates $(x, y)$, math.atan(y / x) has two fatal weaknesses:
y / x crashes with a ZeroDivisionError.math.atan(-1.0) returns $-45^\circ$, which is erroneously in quadrant IV instead of $+135^\circ$!| Feature | math.atan(ratio) |
math.atan2(y, x) |
|---|---|---|
| Arguments | Single ratio (float) |
Separate coordinates: y and x |
| Output Range | $(-rac{\pi}{2}, rac{\pi}{2})$ ($-90^\circ$ to $+90^\circ$, 2 quadrants) | $(-\pi, \pi]$ ($-180^\circ$ to $+180^\circ$, all 4 quadrants) |
| If $x = 0$ | Must handle division by zero manually | Handles cleanly: returns $\pmrac{\pi}{2}$ ($\pm 90^\circ$) |
| Best For | Known slope ratios, civil engineering grades | Navigation, 2D player tracking, game physics |
import math
x, y = -1.0, 1.0 # Quadrant 2 point
print("atan(y / x): ", math.degrees(math.atan(y / x))) # -45.0 degrees (Ambiguous!)
print("atan2(y, x): ", math.degrees(math.atan2(y, x))) # 135.0 degrees (Exact!)
Visualizing the smooth S-curve of $rctan(x)$ across $[-10, 10]$ using Matplotlib:
import math
import matplotlib.pyplot as plt
x_vals = [i * 0.2 - 10.0 for i in range(0, 101)] # -10 to +10
y_vals = [math.atan(x) for x in x_vals]
plt.plot(x_vals, y_vals, label="atan(x)", color="#1a73e8")
plt.axhline(math.pi / 2, color="red", linestyle="--", label="pi / 2 asymptote")
plt.axhline(-math.pi / 2, color="red", linestyle="--", label="-pi / 2 asymptote")
plt.title("Inverse Tangent (Arc Tangent) S-Curve")
plt.xlabel("Input Ratio x")
plt.ylabel("Angle in Radians")
plt.grid(True)
plt.legend()
plt.show()
In highway design and civil engineering, road slope warnings are posted as grade percentages ($10\%$ grade $= rac{10 ext{m rise}}{100 ext{m run}} = 0.10$). To convert road grade percentage into inclination degrees: $$ heta = rctan\left(rac{ ext{grade\_percent}}{100}ight)$$ Let's build a slope grade calculator verified with unit assertions:
import math
def calculate_road_grade_angle(grade_percent):
ratio = grade_percent / 100.0
angle_rad = math.atan(ratio)
angle_deg = math.degrees(angle_rad)
return round(angle_deg, 2)
# Unit Assertions
assert calculate_road_grade_angle(0) == 0.0 # Flat horizontal road
assert calculate_road_grade_angle(6) == 3.43 # 6% interstate warning grade
assert calculate_road_grade_angle(10) == 5.71 # 10% steep mountain road
assert calculate_road_grade_angle(100) == 45.0 # 100% grade (1:1 rise/run) -> 45 degrees
assert calculate_road_grade_angle(-10) == -5.71 # 10% downhill decline
print("6% Highway Grade: ", calculate_road_grade_angle(6), "degrees")
print("10% Mountain Grade:", calculate_road_grade_angle(10), "degrees")
print("All road grade unit assertions passed successfully!")
Open in Google Colab View on GitHub
Run all math.atan() examples interactively, contrast two-quadrant atan with four-quadrant atan2, and verify civil engineering slope grade calculations with unit assertions directly in Google Colab.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.