Skip to main content

# Arctan vs Arctan2

Normally when you calculate the inverse tangent, you are doing so for a question related to a triangle in which the two sides, x and y are lengths (positive numbers), and the resulting angle is between 0 and 90$$^{\circ}$$ ($$\pi$$/2).

However, it turns out that there are two different definitions of the inverse tangent, and which one you use matters if x and y can both be positive or negative. This is exactly the case when using the Cartesian components of location on a sphere to determine the longitude and latitude of that point.

The version of tangent that you find on your calculator is a two-quadrant inverse tangent (atan) and it expects a single input value (y/x). This value can be positive or negative and the output will have the same sign and have values between 0 and $$\pi$$/2 (90 $$^{\circ}$$). In the plot below, atan only gives back results in the right half of the circle.

Now let's think about a point (x,y,z) on the equator of the earth, where z=0. Note, that the ratio of y/x=(−y)/(−x), so that atan will return the same value for the angle (the longitude), but these two points do not lie in the same location on the sphere.

This is why, there is also a four-quadrant inverse tangent, atan2(y,x). Note this version of the inverse tangent requires two input values. This allows the function to determine in which of the four quadrants (+x,+y), (+x,-y), (-x,+y) or (-x,-y) the result should lie. The output of the function range from 0 to ±180$$^{\circ}$$ (±$$\pi$$/2), with positive values (east longitudes) for positive y values, and negative values (west longitudes) for negative y values.

Below, I include the example for x = -1 and y = -1 (blue circle)

* atan returns 45$$^{\circ}$$, because -1/-1 = 1, which is the same as 1/1 (red circle)

* atan2 returns 135$$^{\circ}$$, which is the correct answer accounting for the signs of x and y.

import matplotlib.pyplot as plt
import numpy as np

r2d = 180/np.pi
fig = plt.figure(figsize=(7,7))
ax = fig.add_subplot(1,1,1)
x = np.arange(-1,1.,0.01)
y = np.arange(1,-1.,-0.01)

ycirc = np.sqrt(1 - x**2)
x1 = -1
y1 = -1
print('atan2 for x = -1, y = -1',np.arctan2(y1,x1)*r2d)
print('atan,for x = -1, y = -1',np.arctan(y1/x1)*r2d)

ax.plot(np.zeros(y.shape),y)
ax.plot(x, np.zeros(x.shape))
ax.plot(x, ycirc,'black')
ax.plot(x, -ycirc,'black')
ax.plot([0, -1], [0,-1],color='blue')
ax.plot([0, 1], [0,1],color='red')
ax.plot(1,1,marker='o',color='red')
ax.plot(-1,-1,marker='o',color='blue')

ax.set_aspect('equal')
ax.set_ylim([-1.2, 1.2])
ax.set_xlim([-1.4, 1.2])
ax.text(1,0,'0')
ax.text(0,1.1,'$\pi/2, (90)$')
ax.text(-1.19,0,'$\pm\pi$')
ax.text(-1.39,-0.2,'($\pm180$)')
ax.text(0,-1.1,'$-\pi/2, (-90)$')
ax.text(0.5,0.5,'East')
ax.text(-0.5,0.5,'East')
ax.text(0.5,-0.5,'West')
ax.text(-0.5,-0.5,'West')
ax.set_xlabel('x coordinate')
ax.set_ylabel('y coordinate')
ax.set_title('(x,y) at the equator')
plt.show()

For more information on the math behind arctan, click here, or for arctan2, click here.

Numpy Documentation: Arctan and Arctan2

Another Explanation:

The normal inverse tangent function has a range between -$$\pi$$/2 and $$\pi$$/2, or -90 to 90 degrees. In mathematical notation -$$\pi$$/2 $$\leq \arctan(z) \leq \pi$$/2 or in degrees, -90$$^{\circ} \leq \arctan(z) \leq$$ 90$$^{\circ}$$, where z = y/x = opposite/adjacent. This is because the function cannot distinguish between all four quadrants.

Recall that $$\tan(\theta) = \sin(\theta) / \cos(\theta)$$, and in right triangles $$\tan(\theta) = y / x$$

If $$\tan(\theta)$$ is positive, that could mean that either the angle is in Quadrant I (where  both $$\sin(\theta)$$ and $$\cos(\theta)$$ are positive) or Quadrant III (where both $$\sin(x)$$ and $$\cos(\theta)$$ are negative).

Similarly if $$\tan(\theta)$$ is negative, that could mean that either the angle is in Quadrant II or Quadrant IV, where $$\sin(\theta)$$ and $$\cos(\theta)$$ are opposite signs.

Arctan takes only one input value, and therefore cannot determine which of the two quadrants the angle lies in each case. By default, it provides the angle either in Quadrant I or Quadrant IV depending on the sign of $$\tan(\theta)$$. It is perhaps easier to think of this in terms of triangles, and the one value taken by arctan is really the quotient of the opposite and adjacent sides of the triangle or y/x, so $$\arctan(y/x)$$. In order to determine the correct quadrant, the sign of y and x must be known, however, the function only receives the sign of x/y, and not x and y separately and cannot distinguish quadrants.

This can occasionally be an issue, especially if we are interested in an angle that can range from -180 to 180 degrees, such as longitude. Arctan2 is the 4-quadrant inverse tangent. It can get around the previous issue by taking both x and y in as separate arguments. Whereas the syntax for arctan is $$\arctan(y/x)$$, the syntax for arctan2 is $$\arctan2(y,x)$$. Knowing the signs of x and y separately can determine if the angle lies in any of the four quadrants. The range of arctan2 is -$$\pi$$ $$\leq \arctan2(y,x) \leq \pi$$ or in degrees, -180$$^{\circ} \leq \arctan2(y,x) \leq$$ 180$$^{\circ}$$.

In conclusion, if you are calculating something that ranges between -90 and 90 degrees like latitude, use arctan. If calculating an angle that can be between -180 and 180 degrees, use arctan2.

This explanation for arctan vs arctan2 was adapted and expanded from stackexchange

• Was this article helpful?