If you have ever converted an (x, y) point to an angle and got an answer pointing the wrong way, you have met the main limitation of plain arctan.
The problem with arctan(y / x)
Take the points (1, 1) and (−1, −1). They point in exactly opposite directions, yet
- y / x = 1 / 1 = 1
- y / x = −1 / −1 = 1
Both give arctan(1) = 45°. Dividing y by x throws away the signs, so arctan only ever returns an angle in quadrants I and IV, between −90° and 90°.
How atan2 fixes it
atan2(y, x) takes the two coordinates separately. Because it can see the sign of each one, it knows which quadrant the point is in and returns the full angle in the range (−180°, 180°].
| Point | Quadrant | arctan(y/x) | atan2(y, x) |
|---|---|---|---|
| (3, 4) | I | 53.13° | 53.13° |
| (−3, 4) | II | −53.13° | 126.87° |
| (−3, −4) | III | 53.13° | −126.87° |
| (3, −4) | IV | −53.13° | −53.13° |
The rules behind atan2
atan2 starts from arctan(y / x) and corrects it:
- If x > 0, the answer is arctan(y / x).
- If x < 0 and y ≥ 0, add 180°.
- If x < 0 and y < 0, subtract 180°.
- If x = 0, the answer is +90° when y > 0 and −90° when y < 0.
- atan2(0, 0) is undefined in maths; most programming languages return 0.
Worked example: point (−3, 4)
- The reference angle is arctan(|4| / |−3|) = arctan(1.3333) ≈ 53.13°.
- x is negative and y is positive, so the point is in quadrant II.
- θ = 180° − 53.13° = 126.87°.
You can check this instantly by choosing Point (x, y) in the calculator and entering −3 and 4.
Getting a 0° to 360° angle
atan2 returns negative angles for points below the x-axis. To convert to a bearing between 0° and 360°, add 360° to any negative result. For (3, −4): −53.13° + 360° = 306.87°.
atan2 in code
Almost every language has it, and the argument order is y first:
Math.atan2(4, -3); // JavaScript → 2.2143 rad
math.atan2(4, -3) # Python → 2.2143 rad
Multiply by 180/π to convert radians to degrees: 2.2143 × 57.2958 ≈ 126.87°.
Try it yourself: open the inverse tangent calculator to check any value from this article with full step-by-step working.