Atan2
≈ 11 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
from the origin to the point , confined to . 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 ) 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 .
(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{x^2 + y^2} , then x = r \cos \theta and . If , the desired angle measure is .
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. Using the \operatorname{atan2} function does away with this correction, simplifying code and mathematical formulas.
from the origin to the point , confined to .]] 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 ) 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 .
(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{x^2 + y^2} , then x = r \cos \theta and . If , the desired angle measure is .
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. Using the \operatorname{atan2} function does away with this correction, simplifying code and mathematical formulas. \begin{align} x &= r\cos\theta, & \qquad r &= {\textstyle \sqrt{x^2 + y^2} }, \\[2pt] y &= r\sin\theta, & \tan\theta &= \frac{y}{x}, \end{align} where , , and are trigonometric functions and is the Pythagorean sum of and , i.e. the hypotenuse of a right triangle with legs and .
Computing and from and directly from these formulas is straightforward. In the other direction, though, it is helpful to provide special functions to implement the conversions. The formula to compute from and can lead to numerical overflows and underflows, avoided by the hypot function of many standard libraries. The computation of from and is more complicated, for two reasons: (1) because it could involve a division by zero and (2) because the quotient is equal to , and therefore does not distinguish between point and the opposite point .
The ordinary single-argument arctangent function only returns angle measures in the interval , so a naïve implementation of a conversion from rectangular to polar coordinates, , will yield incorrect results whenever . 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.
To save programmers the trouble, computer programming languages introduced the function, at least as early as the Fortran IV language of the 1960s. 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. In 1961, Fortran introduced the function with argument order so that the argument (phase angle) of a complex number is .
This follows the left-to-right order of a fraction written , so that \operatorname{atan2}(y, x) = \operatorname{atan}(y / x) for positive values of . However, this is the opposite of the conventional component order for complex numbers, , or as coordinates . 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 , OpenOffice Calc uses , and Mathematica uses , 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, \begin{align} \operatorname{atan2}(y, x) &= \operatorname{arg}(x + iy)\\ &= \operatorname{Im}\operatorname{log}(x + iy). \end{align} 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{atan2}(y, x) = \begin{cases} \arctan\left(\frac y x\right) &\text{if } x > 0, \\[5mu] \arctan\left(\frac y x\right) + \pi &\text{if } x 0, \\[5mu] -\frac{\pi}{2} &\text{if } x = 0 \text{ and } y In cases where the function is unavailable or inconvenient to use, and where reducing the number of cases that must be considered is important (e.g. for vectorized computations on a GPU), it can be useful to compute through a tangent half-angle formula: \operatorname{atan2}(y, x) = 2 \arctan \left(\frac{y}{\textstyle \sqrt{x^2 + y^2} + x}\right), except in the case that and , when by convention is equal to . \begin{align} & \frac{\partial}{\partial x}\operatorname{atan2}(y,\, x) = \frac{\partial}{\partial x} \arctan\left(\frac y x \right) = -\frac{y}{x^2 + y^2}, \\[5pt] & \frac{\partial}{\partial y}\operatorname{atan2}(y,\, x) = \frac{\partial}{\partial y} \arctan\left(\frac y x \right) = \frac x {x^2 + y^2}. \end{align} These two partial derivatives are the coordinates of the gradient.
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} \mathrm{d}\theta &= \frac{\partial}{\partial x}\operatorname{atan2}(y,\, x)\,\mathrm{d}x + \frac{\partial}{\partial y}\operatorname{atan2}(y,\, x)\,\mathrm{d}y \\[5pt] &= -\frac{y}{x^2 + y^2}\,\mathrm{d}x + \frac{x}{x^2 + y^2}\,\mathrm{d}y. \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. This figure shows the values of \arctan(\tan(\theta)) along with \operatorname {atan2}(\sin(\theta),\cos(\theta)) for 0\le \theta \le 2\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 \bigl\{\tfrac{\pi}{2},\;\tfrac{3\pi}{2}\bigr\} . The two figures below show 3D views of respectively and over a region of the plane. Note that for , rays in the / -plane emanating from the origin have constant values, but for lines in the / -plane passing through the origin have constant values. For , the two diagrams give identical values.
File:Atan2 diagram.svg|3D view of File:Atan diagram.svg|3D view of \operatorname{atan2} (y_1, x_1) \pm \operatorname{atan2} (y_2, x_2) = \operatorname{atan2} (y_1 x_2 \pm x_1 y_2, x_1 x_2 \mp y_1 y_2), 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 .
As an example, let x_{0}=\frac{\sqrt{3}}{2} and y_{0}=\frac{1}{2} , then the east-counterclockwise format gives \operatorname{atan2}(y_{0}, x_{0})\cdot\frac{180^\circ}{\pi}=30^{\circ} , the north-clockwise format gives \operatorname{atan2}(x_{0}, y_{0})\cdot\frac{180^\circ}{\pi}=60^{\circ} , and the south-clockwise format gives \operatorname{atan2}(-x_{0}, -y_{0})\cdot\frac{180^\circ}{\pi}=-120^{\circ} .
Changing the sign of the - and/or -arguments and/or swapping their positions can create 8 possible variations of the \operatorname{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. In the Intel x86 Architecture assembler code, atan2 is known as the FPATAN (floating-point partial arctangent) instruction.
It can deal with infinities and results lie in the closed interval , e.g. atan2(∞, x) = for finite . Particularly, FPATAN is defined when both arguments are zero: This definition is related to the concept of signed zero. In mathematical writings other than source code, such as in books and articles, the notations Arctan and Tan −1 have been utilized; these are capitalized variants of the regular arctan and tan −1 . This usage is consistent with the complex argument notation, such that .
On HP calculators, treat the coordinates as a complex number and then take the ARG . Or R ARG >> 'ATAN2' STO . On scientific calculators, the function can often be calculated as the angle given when is converted from rectangular coordinates to polar coordinates. Systems supporting symbolic mathematics normally return an undefined value for or otherwise signal that an abnormal condition has arisen.
The free math library FDLIBM (Freely Distributable LIBM) available from netlib has source code showing how it implements atan2 , including handling the various IEEE exceptional values. For systems without a hardware multiplier, the function can be implemented in a numerically reliable manner by the CORDIC method. Thus, implementations of will probably choose to compute . atan2 at Everything2 PicBasic Pro solution atan2 for a PIC18F
Text derived from https://en.wikipedia.org/w/index.php?oldid=1377307668 under CC BY-SA 4.0; modifications have been made.