In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition,
\theta=\operatorname{atan2}(y,x)
-\pi<\theta\leq\pi
x
(x,y)
\operatorname{atan2}(y,x)
x+iy.
The
\operatorname{atan2}
\theta=\operatorname{atan2}(y,x)
x=r\cos\theta
y=r\sin\theta.
If, the desired angle measure is However, when, the angle
\arctan(y/x)
\operatorname{atan2}
The ordinary single-argument arctangent function only returns angle measures in the interval and when invoking it to find the angle measure between the -axis and an arbitrary vector in the Cartesian plane, there is no simple way to indicate a direction in the left half-plane (that is, a point
(x,y)
x<0
y/x=(-y)/(-x),
y/x
To determine an angle measure using the arctangent function given a point or vector
(x,y),
x
x,
y
To remedy this, 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.
In 1961, Fortran introduced the function with argument order
(y,x)
\operatorname{arg}z=\operatorname{atan2}(\operatorname{Im}z,\operatorname{Re}z).
y/x,
\operatorname{atan2}(y,x)=\operatorname{atan}(y/x)
x.
z=x+iy,
(\operatorname{Re}z,\operatorname{Im}z).
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),
\operatorname{arctan2}(x,y),
\operatorname{ArcTan}[x,y],
(-\pi,\pi]
In terms of the standard function, whose range is, it can be expressed as follows to define a surface that has no discontinuities except along the semi-infinite line x<0 y=0:
A compact expression with four overlapping half-planes is
The Iverson bracket notation allows for an even more compact expression:[3]
Formula without apparent conditional construct:
The following expression derived from the tangent half-angle formula can also be used to define :This expression may be more suited for symbolic use than the definition above. However it is unsuitable for general floating-point computational use, as the effect of rounding errors in expand near the region (this may even lead to a division of y by zero).
A variant of the last formula that avoids these inflated rounding errors:
Notes:
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}
\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.
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))
\operatorname{atan2}(\sin(\theta),\cos(\theta))
0\le\theta\le2\pi
\pi
2\pi
\theta
\operatorname{atan2}
\theta=\pi
\arctan
\theta\in\{\tfrac{\pi}{2}, \tfrac{3\pi}{2}\}
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.
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 .
The
atan2
atan2
atan2(y,x),
atan2(x,y),
atan2(-x,-y)
As an example, let
x0=
\sqrt{3 | |
y0=
1 | |
2 |
atan2(y0,x0) ⋅
180 | |
\pi |
=30\circ
atan2(x0,y0) ⋅
180 | |
\pi |
=60\circ
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
The realization of the function differs from one computer language to another:
(\operatorname{Re},\operatorname{Im})
ArcTan[''x'', ''y'']
is used where the one parameter form supplies the normal arctangent. Mathematica classifies ArcTan[0, 0]
as an indeterminate expression.(\operatorname{Re},\operatorname{Im})
angle(x,y)
and although it appears to take two arguments, it really only has one complex argument which is denoted by a pair of numbers: .The
(\operatorname{Im},\operatorname{Re})
atan2
, and most other computer implementations, are designed to reduce the effort of transforming cartesian to polar coordinates and so always define atan2(0, 0)
. On implementations without signed zero, or when given positive zero arguments, it is normally defined as 0. It will always return a value in the range rather than raising an error or returning a NaN (Not a Number).atan
function allows one to optionally supply the x coordinate: (atan ''y'' ''x'')
.[12]atan2
, the language has a one-parameter and a two-parameter form for atan
.[13] However, it has many more than two methods, to allow for aggressive optimisation at compile time (see the section "Why don't you compile Matlab/Python/R/… code to Julia?" [14]).atan2
is known as the FPATAN
(floating-point partial arctangent) instruction.[15] It can deal with infinities and results lie in the closed interval, e.g. atan2(∞, ''x'')
= +/2 for finite x. Particularly, FPATAN
is defined when both arguments are zero:atan2(+0, +0)
= +0;
atan2(+0, −0)
= +;
atan2(−0, +0)
= −0;
atan2(−0, −0)
= −.
This definition is related to the concept of signed zero.
ARG
. Or << C->R ARG >> 'ATAN2' STO
.atan2
including handling the various IEEE exceptional values.undefined ⋅ 0=0,
undefined ⋅ 1=undefined,
z+undefined=undefined
z.