Skip to main content
Geosciences LibreTexts

Arctan vs Arctan2

  • Page ID
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    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(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.set_ylim([-1.2, 1.2])
    ax.set_xlim([-1.4, 1.2])
    ax.text(0,1.1,'$\pi/2, (90)$')
    ax.text(0,-1.1,'$-\pi/2, (-90)$')
    ax.set_xlabel('x coordinate')
    ax.set_ylabel('y coordinate')
    ax.set_title('(x,y) at the equator')

    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?