The function first appeared in the programming language Fortran (in IBM's implementation FORTRAN-IV in 1961). It was originally intended to return a correct and unambiguous value for the angle ? in converting from cartesian coordinates(x,y) to polar coordinates(r,?).
returns a single value ? such that -? < ? ? and, for some r > 0,
It is clear that always, but it is not always accurate to observe
which is correct only when x > 0. When x < 0, the angle apparent from the expression above is pointing in the opposite direction of the correct angle and a value of ? (or 180°) must be either added or subtracted from ? to put the cartesian point (x,y) into the correct quadrant of the Euclidean plane. This requires knowledge of the signs of x and y separately, which is information lost when y is divided by x.
Since any integer multiple of 2π can be added to the angle ? without changing either x or y, implying an ambiguous value for the returned value, the principal value of the angle, in the interval (-?, ?] is returned. ? is signed, with counterclockwise angles being positive, and clockwise being negative. Specifically, is in the interval[0, ?] when y >= 0, and in (−π, 0) when y < 0.
History and motivation
Graph of the tangent function from −? to +? with the corresponding signs of y over x. The green arrows point to the results of atan2(-1,-1) and atan2(1,1).
The atan2 function was first introduced in computer programming languages, but now it is also common in other fields of science and engineering. It dates back at least as far as the FORTRAN programming language
and is currently found in many modern programming languages. Among these languages are: C's math.hstandard library, the Java Math library, .NET's System.Math (usable from C#, VB.NET, etc.), the Python math module, the Ruby Math module, the Golang math package, and elsewhere. In addition, many scripting languages, such as Perl, include the C-style atan2(y,x) function.
The single-argument arctangent function cannot distinguish between diametrically opposite directions. For example, the anticlockwise angle from the x-axis to the vector (1, 1), calculated in the usual way as arctan(1/1), is ?/4 (radians), or 45°. However, the angle between the x-axis and the vector (-1, -1) appears, by the same method, to be arctan(-1/-1), again ?/4, even though one might expect the answers -3?/4, or 5?/4, -135° or 225°. In addition, an attempt to find the angle between the x-axis and the vectors (0, y), y ? 0 requires evaluation of arctan(y/0), which fails on division by zero.
The atan2 function calculates one unique arc tangent value from two variables y and x, where the signs of both arguments are used to determine the quadrant of the result, thereby selecting the desired branch of the arc tangent of y/x, e.g., atan2 (1, 1) = ?/4 and atan2 (-1, -1) = -3?/4. Similarly, e.g., atan2 (1, 0 ) = ?/2.
When calculations are performed manually, the necessary quadrant corrections and exception handling can be done by inspection, but it is more useful to have a single function that always gives an unambiguous correct result.
The atan2 function is useful in many applications involving vectors in Euclidean space, such as finding the direction from one point to another. A principal use is in computer graphics rotations, for converting rotation matrix representations into Euler angles.
Definition and computation
The function atan2 computes the principal value of the argument function applied to the complex numberx+iy. That is, atan2(y, x) = Pr arg(x+iy) = Arg(x+iy). The argument could be changed by an arbitrary multiple of 2? (corresponding to a complete turn around the origin) without making any difference to the angle, but to define atan2 uniquely one uses the principal value in the range, that is, -? < atan2(y, x) ?.
In terms of the standard arctan function, whose range is (-, ), it can be expressed as follows:
A compact expression with four overlapping half-planes is
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 x < 0, y = 0 (this may even lead to a division of y by zero).
A variant of the last formula that avoids these inflated rounding errors:
The derivation of the principal value of the argument refers to this figure
As mentioned above, the principal value of the argument atan2(y, x) can be related to arctan(y/x) by trigonometry. The derivation goes as follows:
If (x,y) = (r cos ?, r sin ?) then tan = . It follows that
Note that + x ? 0 in the domain in question.
As the function atan2 is a function of two variables, it has two partial derivatives. At points where these derivatives exist, atan2 is, except for a constant, equal to arctan(y/x). Hence for x > 0 or y ? 0,
Informally representing the function atan2 as the angle function ?(x, y) = atan2(y, x) (which is only defined up to a constant) yields the following formula for the total differential:
While the function atan2 is discontinuous along the negative x-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.
The partial derivatives of atan2 do not contain trigonometric functions, making it particularly useful in many applications (e.g. embedded systems) where trigonometric functions can be expensive to evaluate.
atan2 for selected rays
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 atan2(y, x) computes the angle corresponding to the point (x, y).
This figure shows the values of alongside with for . Both functions are odd and periodic with periods and , respectively, and thus can easily be supplemented to any region of real values of . One can clearly see the branch cuts of the -function at , and of the -function at .
The two figures below show 3D views of respectively atan2(y, x) and arctan over a region of the plane. Note that for atan2(y, x), rays in the X/Y-plane emanating from the origin have constant values, but for arctanlines in the X/Y-plane passing through the origin have constant values. For x > 0, the two diagrams give identical values.
Angle sum and difference identity
Sums of may be collapsed into a single operation according to the following identity
...provided that .
The proof involves considering two cases, one where or and one where and .
We only consider the case where or . To start, we make the following observations:
To see (4), we have the identity where , hence . Furthermore, since for any positive real value , then if we let and then we have .
From these observations have following equivalences:
Corollary: if and are 2-dimensional vectors, the difference formula is frequently used in practice to compute the angle between those vectors with the help of , since the resulting computation behaves benign in the range and can thus be used without range checks in many practical situations.
Realizations of the function in common computer languages
The realization of the function differs from one computer language to another:
The C function 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).
In Common Lisp, where optional arguments exist, the atan function allows one to optionally supply the x coordinate: (atan yx).
In Mathematica, the form ArcTan[x, y] is used where the one parameter form supplies the normal arctangent (note that the order of arguments is reversed relative to the convention used in the discussion above). Mathematica classifies ArcTan[0, 0] as an indeterminate expression.
In the Intel 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) = +?/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.
On most TI graphing calculators (excluding the TI-85 and TI-86), the equivalent function is called R?P? and has the arguments reversed.
On TI-85 the arg function is called 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: x + yi = (x, y).
In mathematical writings other than source code, such as in books and articles, the notations Arctan and Tan-1 have been utilized; these are uppercase variants of the regular arctan and tan-1. This usage is consistent with the complex argument notation, such that Atan(y, x) = Arg(x + yi).
On HP calculators, treat the coordinates as a complex number and then take the ARG. Or << C->R ARG >> 'ATAN2' STO.
Systems supporting symbolic mathematics normally return an undefined value for atan2(0, 0) or otherwise signal that an abnormal condition has arisen.
For systems implementing signed zero, infinities, or Not a Number (for example, IEEE floating point), it is common to implement reasonable extensions which may extend the range of values produced to include -? and -0. These also may return NaN or raise an exception when given a NaN argument.
For systems implementing signed zero (for example, IEEE floating point), atan2(-0, x), x < 0 poses the risk of returning the value -?, in the case where the implementation of atan2(y, x) fails to properly handle -0 inputs.
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 atan2 can be implemented in a numerically reliable manner by the CORDIC method. Thus implementations of atan(y) will probably choose to compute atan2(y, 1).