Atan2 Explained

In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition,

\theta=\operatorname{atan2}(y,x)

is the angle measure (in radians, with

-\pi<\theta\leq\pi

) between the positive

x

-axis
and the ray from the origin to the point

(x,y)

in the Cartesian plane. Equivalently,

\operatorname{atan2}(y,x)

is the argument (also called phase or angle) of the complex number

x+iy.

(The argument of a function and the argument of a complex number, each mentioned above, should not be confused.)

The

\operatorname{atan2}

function first appeared in the programming language Fortran in 1961. It was originally intended to return a correct and unambiguous value for the angle in converting from Cartesian coordinates to polar coordinates . If

\theta=\operatorname{atan2}(y,x)

and r = \sqrt, then

x=r\cos\theta

and

y=r\sin\theta.

If, the desired angle measure is \theta = \operatorname(y,x) = \arctan\left(y / x \right). However, when, the angle

\arctan(y/x)

is diametrically opposite the desired angle, and (a half turn) must be added to place the point in the correct quadrant.[1] Using the

\operatorname{atan2}

function does away with this correction, simplifying code and mathematical formulas.

Motivation

The ordinary single-argument arctangent function only returns angle measures in the interval, and when invoking it to find the direction angle from the origin to an arbitrary point in the Cartesian plane, it will yield incorrect results when the point is in the left half-plane . Diametrically opposite angle measures have the same tangent because

y/x=(-y)/(-x).

To fully determine the direction angle from the origin given a point using the arctangent function, mathematical formulas or computer code must handle multiple cases; at least one for positive values of

x

and one for negative values of, and sometimes additional cases when is negative or one coordinate is zero. Finding angle measures and converting Cartesian to polar coordinates are common in scientific computing, and this code is redundant and error-prone.

To save programmers the trouble, computer programming languages introduced the function, at least as early as the Fortran IV language of the 1960s.[2] The quantity is the angle measure between the -axis and a ray from the origin to a point anywhere in the Cartesian plane. The signs of and are used to determine the quadrant of the result and select the correct branch of the multivalued function .

The function is useful in many applications involving Euclidean vectors such as finding the direction from one point to another or converting a rotation matrix to Euler angles.

The function is now included in many other programming languages, and is also commonly found in mathematical formulas throughout science and engineering.

Argument order

In 1961, Fortran introduced the function with argument order so that the argument (phase angle) of a complex number is

\operatorname{arg}z=\operatorname{atan2}(\operatorname{Im}z,\operatorname{Re}z).

This follows the left-to-right order of a fraction written

y/x,

so that

\operatorname{atan2}(y,x)=\operatorname{atan}(y/x)

for positive values of

x.

However, this is the opposite of the conventional component order for complex numbers,

z=x+iy,

or as coordinates

(\operatorname{Re}z,\operatorname{Im}z).

See section Definition and computation.

Some other programming languages (see § Realizations of the function in common computer languages) picked the opposite order instead. For example Microsoft Excel uses

\operatorname{Atan2}(x,y),

OpenOffice Calc uses

\operatorname{arctan2}(x,y),

and Mathematica uses

\operatorname{ArcTan}[x,y],

defaulting to one-argument arctangent if called with one argument.

Definition and computation

The function computes the principal argument of the complex number, which is also the imaginary part of the principal value of the complex logarithm. That is,\operatorname(y, x) = \operatorname(x + iy) = \operatorname\operatorname(x + iy).

Adding any integer multiple of (corresponding to complete turns around the origin) gives another argument of the same complex number, but the principal argument is defined as the unique representative angle in the interval .

In terms of the standard arctangent function, whose image is, can be expressed piecewise:

\operatorname(y, x) =\begin \arctan\left(\frac y x\right) &\text x > 0, \\[5mu] \arctan\left(\frac y x\right) + \pi &\text x < 0 \text y \ge 0, \\[5mu] \arctan\left(\frac y x\right) - \pi &\text x < 0 \text y < 0, \\[5mu] +\frac &\text x = 0 \text y > 0, \\[5mu] -\frac &\text x = 0 \text y < 0, \\[5mu] \text &\text x = 0 \text y = 0.\end

Instead of the tangent, it can be convenient to use the half-tangent as a representation of an angle, partly because the angle has a unique half-tangent,\tan\tfrac12\theta = \frac = \frac.

(See tangent half-angle formula.) The expression with in the denominator should be used when and to possible loss of significance in computing . When an function is unavailable, it can be computed as twice the arctangent of the half-tangent .

Derivative

As the function is a function of two variables, it has two partial derivatives. At points where these derivatives exist, is, except for a constant, equal to . Hence for or,

\begin{align} &

\partial
\partialx

\operatorname{atan2}(y,x)=

\partial\arctan\left(
\partialx
y
x

\right)=-

y
x2+y2

,\\[5pt] &

\partial
\partialy

\operatorname{atan2}(y,x)=

\partial\arctan\left(
\partialy
y
x

\right)=

x
x2+y2

. \end{align}

Thus the gradient of atan2 is given by

\nablaatan2(y,x)=\left({-y\overx2+y2},{x\overx2+y2}\right).

Informally representing the function as the angle function (which is only defined up to a constant) yields the following formula for the total differential:

\begin{align} d\theta &=

\partial
\partialx

\operatorname{atan2}(y,x)dx+

\partial
\partialy

\operatorname{atan2}(y,x)dy\\[5pt] &=-

y
x2+y2

dx+

x
x2+y2

dy. \end{align}

While the function is discontinuous along the negative -axis, reflecting the fact that angle cannot be continuously defined, this derivative is continuously defined except at the origin, reflecting the fact that infinitesimal (and indeed local) changes in angle can be defined everywhere except the origin. Integrating this derivative along a path gives the total change in angle over the path, and integrating over a closed loop gives the winding number.

In the language of differential geometry, this derivative is a one-form, and it is closed (its derivative is zero) but not exact (it is not the derivative of a 0-form, i.e., a function), and in fact it generates the first de Rham cohomology of the punctured plane. This is the most basic example of such a form, and it is fundamental in differential geometry.

The partial derivatives of do not contain trigonometric functions, making it particularly useful in many applications (e.g. embedded systems) where trigonometric functions can be expensive to evaluate.

Illustrations

This figure shows values of atan2 along selected rays from the origin, labelled at the unit circle. The values, in radians, are shown inside the circle. The diagram uses the standard mathematical convention that angles increase counterclockwise from zero along the ray to the right. Note that the order of arguments is reversed; the function computes the angle corresponding to the point .

This figure shows the values of

\arctan(\tan(\theta))

along with

\operatorname{atan2}(\sin(\theta),\cos(\theta))

for

0\le\theta\le2\pi

. Both functions are odd and periodic with periods

\pi

and

2\pi

, respectively, and thus can easily be supplemented to any region of real values of

\theta

. One can clearly see the branch cuts of the

\operatorname{atan2}

-function at

\theta=\pi

, and of the

\arctan

-function at

\theta\in\{\tfrac{\pi}{2},\tfrac{3\pi}{2}\}

.[3]

The two figures below show 3D views of respectively and over a region of the plane. Note that for, rays in the X/Y-plane emanating from the origin have constant values, but for lines in the X/Y-plane passing through the origin have constant values. For, the two diagrams give identical values.

Angle sum and difference identity

The sum or difference of multiple angles to be computed by can alternately be computed by composing them as complex numbers. Given two coordinate pairs and, their angles from the positive axis will be composed (and lengths multiplied) if they are treated as complex numbers and then multiplied together, . The resulting angle can be found using a single operation, so long as the as long as the resulting angle lies in :

\operatorname{atan2}(y1,x1)\pm\operatorname{atan2}(y2,x2)=\operatorname{atan2}(y1x2\pmx1y2,x1x2\mpy1y2),

and likewise for more than two coordinate pairs. If the composed angle crosses the negative -axis (i.e. exceeds the range), then the crossings can be counted and the appropriate integer multiple of added to the final result to correct it.

This difference formula is frequently used in practice to compute the angle between two planar vectors, since the resulting angle is always in the range .

East-counterclockwise, north-clockwise and south-clockwise conventions, etc.

The

atan2

function was originally designed for the convention in pure mathematics that can be termed east-counterclockwise. In practical applications, however, the north-clockwise and south-clockwise conventions are often the norm. In atmospheric sciences, for instance, the wind direction can be calculated using the

atan2

function with the east- and north-components of the wind vector as its arguments;[4] the solar azimuth angle can be calculated similarly with the east- and north-components of the solar vector as its arguments. The wind direction is normally defined in the north-clockwise sense, and the solar azimuth angle uses both the north-clockwise and south-clockwise conventions widely.[5] These different conventions can be realized by swapping the positions and changing the signs of the x- and y-arguments as follows:

atan2(y,x),     

(East-Counterclockwise Convention)

atan2(x,y),     

(North-Clockwise Convention)

atan2({-x},{-y})

. (South-Clockwise Convention)

As an example, let

x0=

\sqrt{3
} and

y0=

1
2
, then the east-counterclockwise format gives

atan2(y0,x0)

180
\pi

=30\circ

, the north-clockwise format gives

atan2(x0,y0)

180
\pi

=60\circ

, and the south-clockwise format gives

atan2(-x0,-y0)

180
\pi

=-120\circ

.

Changing the sign of the x- and/or y-arguments and/or swapping their positions can create 8 possible variations of the

atan2

function and they, interestingly, correspond to 8 possible definitions of the angle, namely, clockwise or counterclockwise starting from each of the 4 cardinal directions, north, east, south and west.

Realizations of the function in common computer languages

The realization of the function differs from one computer language to another:

(\operatorname{Re},\operatorname{Im})

(reversed relative to the convention used in the discussion above).

(\operatorname{Re},\operatorname{Im})

.

The

(\operatorname{Im},\operatorname{Re})

convention is used by:

atan2(+0, +0) = +0;

atan2(+0, −0) = +;

atan2(−0, +0) = −0;

atan2(−0, −0) = −.

This definition is related to the concept of signed zero.

See also

External links

Other implementations/code for atan2

Notes

  1. Web site: The argument of a complex number. Winter 2011. Santa Cruz Institute for Particle Physics.
  2. Book: Organick , Elliott I. . 1966 . A FORTRAN IV Primer . Addison-Wesley . Some processors also offer the library function called ATAN2, a function of two arguments (opposite and adjacent). . 42.
  3. Web site: Wolf Jung: Mandel, software for complex dynamics. www.mndynamics.com. 20 April 2018.
  4. Web site: Wind Direction Quick Reference. NCAR UCAR Earth Observing Laboratory.
  5. 10.1016/j.renene.2021.03.047. A solar azimuth formula that renders circumstantial treatment unnecessary without compromising mathematical rigor: Mathematical setup, application and extension of a formula based on the subsolar point and atan2 function. 2021. Zhang. Taiping. Stackhouse. Paul W.. MacPherson. Bradley. Mikovitz. J. Colleen. Renewable Energy. 172. 1333–1340. 233631040. free. 2021REne..172.1333Z .
  6. Web site: Microsoft Excel Atan2 Method. 14 June 2014 . Microsoft.
  7. Web site: LibreOffice Calc ATAN2. Libreoffice.org.
  8. Web site: Functions and formulas – Docs Editors Help. support.google.com.
  9. Web site: Numbers' Trigonometric Function List. Apple .
  10. Web site: CLHS: Function ASIN, ACOS, ATAN. LispWorks.
  11. Web site: Mathematics · The Julia Language. docs.julialang.org.
  12. Web site: Frequently Asked Questions · The Julia Language. docs.julialang.org.
  13. IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.
  14. Book: Principles of Digital Image Processing: Fundamental Techniques. Wilhelm. Burger. Mark J.. Burge. 7 July 2010. Springer Science & Business Media. 978-1-84800-191-6. 20 April 2018. Google Books.
  15. Book: Glisson, Tildon H.. Introduction to Circuit Analysis and Design. 18 February 2011. Springer Science & Business Media. 9789048194438. 20 April 2018. Google Books.