Home Python math Functions atan2() Python math.atan2() Function Definition math.atan2(y, x) computes the angle in radians between the positive x-axis and the point (x, y), returning values across the entire circle from -pi to pi.
Because you pass y and x separately (notice y comes first! ), atan2() picks the exact quadrant and works cleanly even when x is zero.
math.atan2() is a function in Python's standard math module: import it with import math and call math.atan2(...), or import the name directly with from math import atan2.
Syntax Import math and pass y first, x second.
import math
theta = math.atan2(y, x) Call What it does math.atan2(1, 0)Returns approximately 1.570796... (+90 deg). math.atan2(-1, -1)Returns approximately -2.356194... (-135 deg). math.atan2(0.0, 0.0)Returns 0.0. math.atan2(-0.0, -1.0)Returns -3.141592653589793 (-math.pi).
Double-check the argument order: math.atan2(y, x) puts vertical y first and horizontal x second, unlike Cartesian pairs (x, y).
Function signatures math.atan2(y, x, /)
# returns: float angle in [-pi, pi] for point (x, y) Parameters Parameter Description yRequired positional vertical (sine-side) coordinate. xRequired positional horizontal (cosine-side) coordinate.
Return Value Return Value Description floatAngle in radians in [-math.pi, math.pi].
Important
Swapping x and y is the classic bug with atan2(y, x). It never raises ZeroDivisionError when x == 0, and math.atan2(-0.0, -1.0) yields -pi while math.atan2(0.0, -1.0) yields +pi.
Python Examples 1. Four quadrants 2. Compass bearing 3. Signed zero branch cut Example 1 — Aim a Game Turret Across All Four Quadrants Compute signed target angles from -180 to +180 degrees including vertical axes.
import math
targets = [(1, 1), (1, 0), (-1, 1), (-1, -1), (1, -1)]
for dx, dy in targets:
deg = math.degrees(math.atan2(dy, dx))
print(f"dx={dx:>2}, dy={dy:>2} -> {deg:>6.1f} deg") dx= 1, dy= 1 -> 45.0 deg
dx= 1, dy= 0 -> 0.0 deg
dx=-1, dy= 1 -> 135.0 deg
dx=-1, dy=-1 -> -135.0 deg
dx= 1, dy=-1 -> -45.0 deg How It Works y before x in the call. Even though targets are stored as (dx, dy), the call passes math.atan2(dy, dx).Zero y gives 0.0. Pointing straight right along +x evaluates cleanly to 0.0 degrees.All quadrants separated. Upper-left gives +135.0 and lower-left gives -135.0 with zero ambiguity.Example 2 — Convert 2D Velocity Vectors to 0-360 Compass Bearings Transform counterclockwise math angles into clockwise North-up bearings in [0, 360).
import math
headings = [("North", 0, 1), ("East", 1, 0), ("South", 0, -1), ("West", -1, 0)]
for label, vx, vy in headings:
math_deg = math.degrees(math.atan2(vy, vx))
bearing = (90.0 - math_deg) % 360.0
print(f"{label:>5}: {bearing:>5.1f} deg") North: 0.0 deg
East: 90.0 deg
South: 180.0 deg
West: 270.0 deg How It Works Zero division avoided. Due North and due South both have vx == 0, which atan2() handles without errors.Rotate origin to North. Subtracting counterclockwise angle from 90.0 turns East (0) into 90 and North (90) into 0.Wrap modulo 360. Using % 360.0 keeps West (-90 -> 270.0) cleanly inside [0, 360).Example 3 — Inspect Branch Cut Behavior With Signed Zeros Compare atan2(0.0, -1.0) and atan2(-0.0, -1.0) on the negative x-axis.
import math
print(f"{math.atan2(0.0, -1.0):.4f}")
print(f"{math.atan2(-0.0, -1.0):.4f}")
print(math.atan2(0.0, 0.0), math.atan2(1.0, 0.0) == math.pi / 2.0) How It Works Positive zero on negative axis. Lying on the top side of the branch cut yields +pi.Negative zero flips to -pi. IEEE signed zero shifts to the bottom edge of the branch cut at -pi.Origin and vertical axis. Both (0, 0) and straight up (0, 1) return well-defined finite angles.Frequently Asked Questions Why does math.atan2(y, x) take y first and x second? It preserves the numerator-then-denominator order of the slope y / x from math.atan(y / x), matching C libm atan2(y, x).
Does math.atan2(y, 0) raise ZeroDivisionError? No. When x is 0, math.atan2(1, 0) returns pi/2 and math.atan2(-1, 0) returns -pi/2 cleanly.
What does math.atan2(0, 0) return? 0.0, following IEEE 754 convention.
How do I turn math.atan2() into 0 to 360 degrees? Compute deg = math.degrees(math.atan2(y, x)) % 360.0 for counterclockwise degrees from +x, or (90.0 - deg) % 360.0 for clockwise compass bearings.
Why do math.atan2(0.0, -1.0) and math.atan2(-0.0, -1.0) have opposite signs? Because -pi and +pi meet along the negative x-axis, and the sign bit of y decides which side of the cut is returned.
Next: math.atanh() Return the inverse hyperbolic tangent of x in (-1, 1) as a float.
atanh() → 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.
13 people found this page helpful
Helpful Share Copy link Suggestion