Compute the two-argument arctangent, returning the angle of the point (x, y) in radians.
ATAN2(y, x)
## Overview Returns the counterclockwise angle in radians from the positive x-axis to the ray that points from the origin to the point (x, y). Unlike ATAN, which uses a single ratio argument and collapses quadrants, ATAN2 uses the signs of both arguments to return an unambiguous angle in (-pi, pi]. This makes it the correct choice whenever you have raw Cartesian components rather than a precomputed slope. ATAN2 is a foundational primitive in navigation, robotics, physics simulation, and geospatial analytics. Every great-circle bearing calculation, every polar-to-Cartesian inversion, and every compass heading derivation from (north, east) components runs through ATAN2. ## Behavior - Arguments are ordered (y, x), not (x, y). Swapping them reflects the result across the line y = x. - Returns a DOUBLE in the closed-open interval (-pi, pi]. - Returns NULL if either argument is NULL. - ATAN2(0, 0) is implementation-defined but conventionally returns 0. - Handles all quadrants correctly, including the axes. ATAN2(0, -1) returns pi, not 0. - Is continuous everywhere except at the negative x-axis, where the output jumps from -pi to pi. ## Numeric precision - Computed using the IEEE 754 double-precision atan2 routine, accurate to within one ULP for typical inputs. - Extracts phase information reliably even when the magnitudes of x and y differ by many orders of magnitude. - Avoids the overflow and division-by-zero issues that affect ATAN(y/x). ## Compatibility - Conforms to the SQL standard definition of ATAN2 with argument order (y, x). - Matches typical mathematical library implementations.
| Name | Type | Description |
|---|---|---|
y | Specifies the y-coordinate (ordinate) of the point. Accepts any finite DOUBLE value. The sign of y combined with the sign of x selects the quadrant of the result. | |
x | Specifies the x-coordinate (abscissa) of the point. Accepts any finite DOUBLE value, including zero. Unlike ATAN(y/x), a zero x does not raise a division error. |
-- Angle to point (1, 1) is pi/4 (45 degrees)
SELECT ATAN2(1, 1);
-- Result: 0.7853981633974483
-- Positive y, zero x -> pi/2 (90 degrees)
SELECT ATAN2(1, 0);
-- Result: 1.5707963267948966
-- Negative x-axis: angle to (-1, 0) is pi
SELECT ATAN2(0, -1);
-- Result: 3.141592653589793
-- Third quadrant (both negative)
SELECT DEGREES(ATAN2(-1, -1));
-- Result: -135.0
-- Origin returns zero (implementation-defined but widely standard)
SELECT ATAN2(0, 0);
-- Result: 0.0
-- Column use: compute heading in degrees from (north, east) deltas
SELECT route_id, (DEGREES(ATAN2(east_delta, north_delta)) + 360) % 360 AS compass_bearing
FROM logistics.routes.legs;