atan2

In a variety of computer languages, the function atan2 is the arctangent function with two arguments. The purpose of using two arguments instead of one is to gather information on the signs of the inputs in order to return the appropriate quadrant of the computed angle, which is not possible for the single-argument arctangent function.

For any real number (e.g., floating point) arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive x-axis of a plane and the point given by the coordinates (x, y) on it. The angle is positive for counter-clockwise angles (upper half-plane, y > 0), and negative for clockwise angles (lower half-plane, y < 0).

History and motivation

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[1] and is currently found in C's math.h standard library, the Java Math library, .NET's System.Math (usable from C#, VB.NET, etc.), the Python math module, the Ruby Math module, and elsewhere. Many scripting languages, such as Perl, include the C-style atan2 function.[2]

In mathematical terms, atan2 computes the principal value of the argument function applied to the complex number x+iy. That is, atan2(y, x) = Pr arg(x+iy) = Arg(x+iy). The argument can be changed by (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) ≤ π.

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.

In some computer programming languages, the order of the parameters is reversed (for example, in some spreadsheets) or a different name is used for the function (for example, Mathematica uses ArcTan[x,y]). On scientific calculators the function can often be calculated as the angle given when (x, y) is converted from rectangular coordinates to polar coordinates. The one-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 the answer clearly should be −3π/4, or −135°.

The atan2 function takes into account the signs of both vector components, and places the angle in the correct quadrant. Thus, atan2(1, 1) = π/4 and atan2(−1, −1) = −3π/4.

Additionally, the ordinary arctangent method breaks down when required to produce an angle of ±π/2 (or ±90°). For example, an attempt to find the angle between the x-axis and the vector (0, 1) requires evaluation of arctan(1/0), which fails on division by zero. In contrast, atan2(1, 0) gives the correct answer of π/2.

When calculations are performed manually, the necessary quadrant corrections and exception handling can be done by inspection, but in computer programs it is extremely useful to have a single function that always gives an unambiguous correct result.

Definition and computation

In terms of the standard arctan function, whose range is (−π/2, π/2), it can be expressed as follows:

\operatorname{atan2}(y, x) = \begin{cases}
\arctan\frac y x & \qquad x > 0 \\
\arctan\frac y x + \pi& \qquad y \ge 0 , x < 0 \\
\arctan\frac y x - \pi& \qquad y < 0 , x < 0 \\
+\frac{\pi}{2} & \qquad y > 0 , x = 0 \\
-\frac{\pi}{2} & \qquad y < 0 , x = 0 \\
\text{undefined} & \qquad y = 0, x = 0
\end{cases}

Notes:

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).

The following expression derived from the tangent half-angle formula can also be used to define atan2.


 \operatorname{atan2} (y, x)=2 \arctan \frac{y}{\sqrt{x^2+y^2}+x}.

This expression may be more suited for symbolic use than the definition above. However it is unsuitable for floating point computational use as it is undefined for y = 0, x < 0 and may overflow near these regions. The formula gives an NaN or raises an error for atan2(0, 0), but this is not an issue since atan2(0, 0) is not defined.

A variant of the last formula is sometimes used in high precision computation. This avoids overflow but is always undefined when y = 0:


 \operatorname{atan2} (y, x)=2 \arctan \frac{\sqrt{x^2+y^2}-x}{y}.

Variations and notation

Illustrations

atan2 round a circle

The diagram alongside shows values of atan2 at selected points on the unit circle. The values, in radians, are shown inside the circle. The diagram uses the standard mathematical convention that angles increase anticlockwise (counterclockwise), and zero is 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).

The diagram below shows values of atan2 for points on the unit circle. On the x-axis is the complex angle of the points, starting from 0 ( point (1,0) ) and going anticlockwise (counterclockwise), through points:

to (1, 0) with complex angle 0 = (2nπ mod 2π).

On this diagram one can clearly see the discontinuity of the atan2 function.[10]

The diagrams below show 3D view of respectively atan2(y, x) and arctan(y/x) over a region of the plane.

Note that for atan2, rays emanating from the origin have constant values, but for atan lines passing through the origin have constant values. For x > 0, the two diagrams give identical values.

Derivative

For more details on this topic, see Winding number.

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:


\frac{\partial\operatorname{atan2}(y,x)}{\partial x} =\frac{\partial\arctan(y/x)}{\partial x} = -\frac{y}{x^2 + y^2}

\frac{\partial\operatorname{atan2}(y,x)}{\partial y} =\frac{\partial\arctan(y/x)}{\partial y} = \ \frac{x}{x^2 + y^2}

for x > 0\,\text{ or }\,y \neq 0.

Informally representing the function atan2 as the angle function \theta(x,y) = \operatorname{atan2}(y,x) (which is only defined up to a constant) yields the following formula for the total derivative:

\begin{align}
d\theta
&= \partial_x\left(\operatorname{atan2}(y,x)\right) dx + \partial_y\left(\operatorname{atan2}(y,x)\right) dy \\
&= -\frac{y}{x^2 + y^2} dx + \frac{x}{x^2 + y^2} dy
\end{align}

While the function atan2 is discontinuous along the negative y-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 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.

References

  1. Organick, Elliott I. (1966). A FORTRAN IV Primer. Addison-Wesley. p. 42. Some processors also offer the library function called ATAN2, a function of two arguments (opposite and adjacent).
  2. The Linux Programmer's Manual says:
    "The atan2() function calculates the arc tangent of the two variables y and x. It is similar to calculating the arc tangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result."
  3. "CLHS: Function ASIN, ACOS, ATAN". LispWorks.
  4. "Atan2 Method". Microsoft.
  5. "ATAN2". Libreoffice.org.
  6. "Function list". Google.
  7. IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.
  8. http://books.google.com/books?id=2LIMMD9FVXkC&pg=PA234&dq=four+quadrant+inverse+tangent+mathematical+notation&hl=en&sa=X&ei=Q2Y4UaGTAcmzyAHsooCoBw&ved=0CDgQ6AEwAg#v=onepage&q=four%20quadrant%20inverse%20tangent%20mathematical%20notation&f=false
  9. http://books.google.com/books?id=7nNjaH9B0_0C&pg=PA345&dq=four+quadrant+inverse+tangent+mathematical+notation&hl=en&sa=X&ei=Q2Y4UaGTAcmzyAHsooCoBw&ved=0CDIQ6AEwAQ#v=onepage&q=four%20quadrant%20inverse%20tangent%20mathematical%20notation&f=false
  10. Computation of the external argument by Wolf Jung

External links

Other implementations/code for atan2