Atan2

Atan2
In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition, is the angle measure (in radians, with ) between the positive -axis and the ray from the origin to the point in the Cartesian plane. Equivalently, is the argument (also called phase or angle) of the complex number
The 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 and , then and
If , the desired angle measure is However, when , the angle is diametrically opposite the desired angle, and ± (a half turn) must be added to place the point in the correct quadrant. Using the 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 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 with ). Diametrically opposite angle measures have the same tangent because so the tangent is not in itself sufficient to uniquely specify an angle.
To determine an angle measure using the arctangent function given a point or vector mathematical formulas or computer code must handle multiple cases; at least one for positive values of 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 remedy this, 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.
Argument order
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 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 value of the argument function applied to the complex number . That is, . The argument could be changed by an arbitrary multiple of (corresponding to a complete turn around the origin) 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 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:
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:
This produces results in the range .
As mentioned above, the principal value of the argument can be related to by trigonometry. The derivation goes as follows: If , then . It follows that Note that in the domain in question.
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 ,
Thus the gradient of atan2 is given by
Informally representing the function as the angle function (which is only defined up to a constant) 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 (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 along 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:
provided that or .
, where is the complex argument function.
whenever , a consequence of Euler's formula.
.
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.
East-counterclockwise, north-clockwise and south-clockwise conventions, etc.
The 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 function with the east- and north-components of the wind vector as its arguments; 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. These different conventions can be realized by swapping the positions and changing the signs of the x- and y-arguments as follows:
(East-Counterclockwise Convention)
(North-Clockwise Convention)
. (South-Clockwise Convention)
As an example, let and , then the east-counterclockwise format gives , the north-clockwise format gives , and the south-clockwise format gives .
Apparently, changing the sign of the x- and/or y-arguments and swapping their positions can create 8 possible variations of the 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:
In Microsoft Excel, OpenOffice.org Calc, LibreOffice Calc, Google Spreadsheets, iWork Numbers, and ANSI SQL:2008 standard, the 2-argument arctangent function has the two arguments in the standard sequence (reversed relative to the convention used in the discussion above).
In Mathematica, the form ArcTan[x,&hairsp;y] is used where the one parameter form supplies the normal arctangent. Mathematica classifies ArcTan[0,&hairsp;0] as an indeterminate expression.
On most TI graphing calculators (excluding the TI-85 and TI-86), the equivalent function is called R►Pθ and has the arguments .
On TI-85 the 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: .
The convention is used by:
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 y x).
In Julia, the situation is similar to Common Lisp: instead of atan2, the language has a one-parameter and a two-parameter form for atan. 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?" ).
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 when = −0. These also may return NaN or raise an exception when given a NaN argument.
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) = +/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.
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 << C->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 .
See also
hypot
References
External links
ATAN2 Online calculator
Java 1.6 SE JavaDoc
atan2 at Everything2
PicBasic Pro solution atan2 for a PIC18F
Other implementations/code for atan2
Notes
Inverse trigonometric functions