Python math.atan2() Function

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

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.

python
import math

theta = math.atan2(y, x)
CallWhat 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
python
math.atan2(y, x, /)
# returns: float angle in [-pi, pi] for point (x, y)

Parameters

ParameterDescription
yRequired positional vertical (sine-side) coordinate.
xRequired positional horizontal (cosine-side) coordinate.

Return Value

Return ValueDescription
floatAngle in radians in [-math.pi, math.pi].

Python Examples

Example 1 — Aim a Game Turret Across All Four Quadrants

Compute signed target angles from -180 to +180 degrees including vertical axes.

python
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")

How It Works

  1. y before x in the call. Even though targets are stored as (dx, dy), the call passes math.atan2(dy, dx).
  2. Zero y gives 0.0. Pointing straight right along +x evaluates cleanly to 0.0 degrees.
  3. 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).

python
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")

How It Works

  1. Zero division avoided. Due North and due South both have vx == 0, which atan2() handles without errors.
  2. Rotate origin to North. Subtracting counterclockwise angle from 90.0 turns East (0) into 90 and North (90) into 0.
  3. 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.

python
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

  1. Positive zero on negative axis. Lying on the top side of the branch cut yields +pi.
  2. Negative zero flips to -pi. IEEE signed zero shifts to the bottom edge of the branch cut at -pi.
  3. Origin and vertical axis. Both (0, 0) and straight up (0, 1) return well-defined finite angles.

Frequently Asked Questions

It preserves the numerator-then-denominator order of the slope y / x from math.atan(y / x), matching C libm atan2(y, x).
No. When x is 0, math.atan2(1, 0) returns pi/2 and math.atan2(-1, 0) returns -pi/2 cleanly.
0.0, following IEEE 754 convention.
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.
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

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