Atan2


The function or is defined as the angle in the Euclidean plane, given in radians, between the positive x axis and the ray to the
The function first appeared in the programming language Fortran. It was originally intended to return a correct and unambiguous value for the angle in converting from cartesian coordinates to polar coordinates.
Equivalently, is the argument of the complex number
returns a single value such that and, for some,
While it is true that, the following equivalence does not always hold:
This only holds when. When, the angle apparent from the expression above is pointing in the opposite direction of the correct angle, and a value of must be either added or subtracted from to put the cartesian point into the correct quadrant of the Euclidean plane. This requires knowledge of the signs of and separately, which is information lost when is divided by.
Since any integer multiple of can be added to the angle without changing either or, 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 when, and in when.

History and motivation

The 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.h standard library, the Java Math library,.NET's System.Math, 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 function.
The single-argument arctangent function cannot distinguish between diametrically opposite directions. For example, the anticlockwise angle from the axis to the vector, calculated in the usual way as, is , or. However, the angle between the axis and the vector appears, by the same method, to be, again, even though one might expect the answers or . In addition, an attempt to find the angle between the axis and the vectors requires evaluation of, which fails on division by zero.
The function calculates one unique arc tangent value from two variables and, 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, e.g., and. Similarly, e.g.,.
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 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 computes the principal value of the argument function applied to the complex number. That is,. The argument could be changed by an arbitrary multiple of without making any difference to the angle, but to define uniquely one uses the principal value in the range, that is,.
In terms of the standard function, whose range is, it can be expressed as follows:
A compact expression with four overlapping half-planes is
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 .
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,
Thus the gradient of atan2 is given by
Informally representing the function as the angle function yields the following formula for the total differential:
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 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 but not exact, 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 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 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 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

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:
  1. provided that or.
  2. , where is the complex argument function.
  3. whenever, a consequence of Euler's formula.
  4. .
To see, 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: