You are on page 1of 9

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

1 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(y,x) 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 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) ≤ π.
The atan2 function is useful in many applications involving vectors in Euclidean space, such as finding the direc-
tion 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 quad-
rant. 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.

2 Definition and computation

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

1
2 2 DEFINITION AND COMPUTATION



arctan( xy ) ifx > 0,




y
≥ 0,
arctan( x ) + π

ifx < 0 and y
arctan( y ) − π ifx < 0 and y < 0,
atan2(y, x) = x
+ π2
 ifx = 0 and y > 0,



− π2 ifx = 0 and y


< 0,

undefined ifx = 0 and y = 0.

Notes:

• This produces results in the range (−π, π], which can be mapped to [0, 2π) by adding 2π to negative results.
• Traditionally, atan2(0, 0) is undefined.
• 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).
• 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 returns the value
−π. atan2(+0, x), x < 0 still returns +π.

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:

 ( )

2 arctan √ y
ifx > 0 or y ̸= 0,
 x2 +y 2 +x
atan2(y, x) =

π ifx < 0 and y = 0,

undefined ifx = 0 and y = 0.

This expression may be more suited for symbolic use than the definition above. However it is unsuitable for floating
point computational use as it may overflow near the region x < 0, y = 0. The formula gives a 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 which avoids overflow is sometimes used in high precision computation:

 (√ )

 x2 +y 2 −x
ify ̸= 0,

2 arctan


y

atan2(y, x) = 0 ifx > 0 and y = 0,





π ifx < 0 and y = 0,


undefined ifx = 0 and y = 0.

2.1 Derivation of atan2(y, x)


The derivation of the principal value of the argument, atan2(y, x) goes as follows:
3

The derivation of atan2(y, x) refers to this figure.

If (x,y) = (r cos θ, r sin θ) then tan θ/2 = y/r + x. It follows that:

θ y
arg(x, y) = θ = 2 = 2 arctan √ .
2 x + y2 + x
2

Note that √x2 + y2 + x ≠ 0 in the domain in question. Computation gives

( )
−y x
∇ arg(x, y) = , .
x2 + y 2 x2 + y 2

3 Variations and notation


• In Common Lisp, where optional arguments exist, the atan function allows one to optionally supply the x
coordinate: (atan y x).[3]
• In Mathematica, the form ArcTan[x, y] is used where the one parameter form supplies the normal arctangent.
Mathematica classifies ArcTan[0, 0] as an indeterminate expression.
• In Microsoft Excel,[4] OpenOffice.org Calc, LibreOffice Calc,[5] Google Spreadsheets,[6] iWork Numbers,[7]
and ANSI SQL:2008 standard,[8] the atan2 function has the two arguments reversed.
4 5 DERIVATIVE

• In the Intel Architecture assembler code, atan2 is known as the FPATAN (floating-point partial arctangent)
instruction.[9] 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[10] and
Tan−1[11] have been utilized; these are uppercase variants of the regular arctan and tan−1 . This usage is con-
sistent 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.

4 Illustrations
The figure 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 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 figure below shows values of atan2 for points on the unit circle. On the x-axis is the angle of the points, starting
from 0 (point (1, 0)) and going counterclockwise, through points:

• (0, 1) with angle π/2 (in radians);

• (−1, 0) with angle π;

• (0, −1) with angle 3π/2;

to (1, 0) with angle 0 = (2πn mod 2π). One can clearly see the discontinuity of the atan2 function.[12]
The two figures below show 3D view of respectively atan2(y, x) and arctan(y/x) over a region of the plane.
Note that for atan2(y, x), rays emanating from the origin have constant values, but for arctan(y/x) lines passing through
the origin have constant values. For x > 0, the two diagrams give identical values.

5 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 for x > 0 or y ≠ 0,

∂ ∂ (y) y
atan2(y, x) = arctan =− 2 ,
∂x ∂x x x + y2
5

atan2(√3, 1) = π/3
(1, √3)
atan2(1, 0) = π/2
(0, 1)

( , )
π/2
π/3

π/3 (=60°)

π 0 (1, 0)
The limit of atan2 atan2(0, 1) = 0
from this side is −π

−π/2

(0,-1)
atan2 at selected points.

∂ ∂ (y) x
atan2(y, x) = arctan = 2 .
∂y ∂y x x + y2
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 derivative:

∂ ∂
dθ = atan2(y, x) dx + atan2(y, x) dy
∂x ∂y
y x
=− 2 dx + 2 dy.
x + y2 x + y2
While the function atan2 is discontinuous along the negative y-axis, reflecting the fact that angle cannot be contin-
uously 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.
6 7 REFERENCES

2pi
atan2

pi

pi/2
angle in radians

-pi/2

-pi

-2pi
pi/2 pi 3pi/2 2pi
angle in radians

atan2 on the unit circle.

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 appli-
cations (e.g. embedded systems) where trigonometric functions can be expensive to evaluate.

6 See also
• hypot

7 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

atan2(y, x) graph.

[7] “iWork '09: ATAN2”. Apple.

[8] “ANSI Compliance”. Teradata.

[9] IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.

[10] 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

[11] 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

[12] Computation of the external argument by Wolf Jung

8 External links
• Java 1.6 SE JavaDoc

• atan2 at Everything2

• PicBasic Pro solution atan2 for a PIC18F


8 8 EXTERNAL LINKS

arctan(y/ x) graph.

8.1 Other implementations/code for atan2


• Bearing Between Two Points
• Arctan and Polar Coordinates

• What’s 'Arccos’?
9

9 Text and image sources, contributors, and licenses


9.1 Text
• Atan2 Source: https://en.wikipedia.org/wiki/Atan2?oldid=700782780 Contributors: SimonP, JohnOwens, Michael Hardy, Stevenj, Dcoet-
zee, Gandalf61, Mattflaschen, Tobias Bergemann, DavidCary, Vadmium, Chowbok, MarkSweep, CALR, ArnoldReinhold, Army1987,
John Vandenberg, Anthony Appleyard, Apatterno, Keenan Pepper, PAR, Kenyon, Simetrical, Armando, GregorB, Cooperised, Ger-
brant, Michel BUZE, Kri, Travis Wells, DVdm, Bgwhite, KSmrq, Geertivp, Arichnad, Dhollm, Avraham, SmackBot, Adam majewski,
Lukas Mach, InverseHypercube, Mdd4696, Betacommand, Bluebot, Nbarth, CDV, Bob K, V1adis1av, Maulattu, Hgilbert, Nijdam,
Csc14us, Utopianheaven, Kpengboy, Ian Vaughan, Mraalex, CBM, Basawala, Myasuda, Davidhorman, Daimanta, Ben pcc, JAnDbot,
Mwarren us, R27182818, Slithymatt, Raise exception, CompuChip, Diegoaac, Rjgodoy, Dmcq, Quietbritishjim, Soler97, Mateat, Cffk,
Jvohn, Cheesefondue, Rumping, Quelt42, Ktr101, NevemTeve, Qwfp, Baudway, Addbot, Fgnievinski, Thonord, Balabiot, Luckas-bot,
Yobot, AnomieBOT, 1exec1, Materialscientist, PreviousDeclaration, ArthurBot, Drilnoth, James Skinsale, FrescoBot, , Maggyero,
Unbitwise, Jfmantis, Yuzisee, Elee, Netheril96, Slawekb, Ὁ οἶστρος, Kuashio, Zephyrus Tavvier, ClueBot NG, KlappCK, Matthiaspaul,
RahulWaghamare, Cerabot~enwiki, Rgfibe, Rjamisolajr, SciKal and Anonymous: 92

9.2 Images
• File:Atan2-discontinuity.svg Source: https://upload.wikimedia.org/wikipedia/commons/8/8f/Atan2-discontinuity.svg License: CC BY-
SA 3.0 Contributors:
• Atan2.PNG Original artist: Atan2.PNG: Adam majewski
• File:Atan2Diagram.png Source: https://upload.wikimedia.org/wikipedia/commons/f/f6/Atan2Diagram.png License: Public domain
Contributors: A Thinkpad. Original artist: Self.
• File:Atan2_60.svg Source: https://upload.wikimedia.org/wikipedia/commons/0/03/Atan2_60.svg License: CC BY-SA 3.0 Contributors:
Own work Original artist: Dmcq
• File:AtanDiagram.png Source: https://upload.wikimedia.org/wikipedia/commons/2/23/AtanDiagram.png License: Public domain Con-
tributors: A Thinkpad. Original artist: Self.
• File:The_principal_value_of_the_argument_(“atan2\char"0022\relax{}_in_some_circles).jpg Source: https://upload.wikimedia.
org/wikipedia/commons/4/40/The_principal_value_of_the_argument_%28%22atan2%22_in_some_circles%29.jpg License: CC BY-
SA 3.0 Contributors: Own work Original artist: SciKal

9.3 Content license


• Creative Commons Attribution-Share Alike 3.0

You might also like