When to use atan2 instead of atan in Python?

I am calculating the formula f = arctan(ImZ / ReZ) and have two options:

Option 1 (atan):

ImZ = -4.593172163003
ReZ = -4.297336384845

z = ImZ / ReZ
f1 = math.atan(z)
print(f1)

Output:

0.8186613519278327

Option 2 (atan2):

f2 = math.atan2(ImZ, ReZ)
print(f2)

Output:

-2.3229313016619604

Why do these two results differ, and when should I choose atan2 over atan in Python atan2?

Use atan2 when handling both X and Y coordinates:

The key difference between atan and atan2 is that atan only takes a single argument (the ratio of the Y-coordinate to the X-coordinate), while atan2 takes both X and Y coordinates separately. This allows atan2 to handle the correct quadrant of the angle, which is particularly useful when working with 2D coordinates or complex numbers.

Example:

import math

ImZ = -4.593172163003
ReZ = -4.297336384845

# Option 1 (atan) – Incorrectly assumes the angle is in the first or fourth quadrant
z = ImZ / ReZ
f1 = math.atan(z)
print(f1)  # Output: 0.8186613519278327

# Option 2 (atan2) – Correctly handles the angle based on both coordinates
f2 = math.atan2(ImZ, ReZ)
print(f2)  # Output: -2.3229313016619604

In this case, atan2 correctly handles the sign of both ImZ and ReZ, returning a value that accounts for the angle in the correct quadrant.

Building on Dimple’s point, use atan2 for consistent results across all quadrants. While atan is limited to returning angles between -π/2 and π/2, atan2 provides results between -π and π. This means atan can’t differentiate between Quadrant II and Quadrant IV when the ratio is negative, leading to potentially misleading values.

Meanwhile, atan2, by taking both X and Y into account, delivers the full range of angles, making it indispensable for reliable calculations in any quadrant.

Example:

ImZ = 5
ReZ = -5

# Using atan only gives the angle in one quadrant
angle_atan = math.atan(ImZ / ReZ)  # result: -0.7853981633974483

# Using atan2 gives the correct angle considering both x and y
angle_atan2 = math.atan2(ImZ, ReZ)  # result: 2.356194490192345

Here, atan2 correctly identifies the angle as being in Quadrant II, while atan produces a result that doesn’t account for the sign of X and Y together.

To add to what Joe and Shashank mentioned, use atan for simplicity when only the ratio of Y to X is needed. If you already have the Y/X ratio pre-computed (like from an earlier calculation), atan can be a simpler and faster option.

That said, if you’re starting with separate X and Y values or want to avoid any ambiguity about quadrants, atan2 is your go-to.

Example:

y = 3
x = 4
ratio = y / x

# atan works when you only have the ratio
angle_atan = math.atan(ratio)  # result: 0.643501109 rad

# atan2 works when you have both x and y separately
angle_atan2 = math.atan2(y, x)  # result: 0.643501109 rad

While both return the same result in this specific case (first quadrant), atan2 ensures you always consider the full context of both X and Y. It’s all about what your situation requires.