Last updated

atan2(y,x) returns the angle th between the ray to the point (x,y) and the positive x-axis, confined to (-p, p]. Atan2definition.svg
atan2(y,x) returns the angle θ between the ray to the point (x,y) and the positive x-axis, confined to (-π, π].
Graph of
{\displaystyle \operatorname {atan2} (y,x)}
{\displaystyle y/x} Arctangent2.svg
Graph of over

The function or (from "2-argument arctangent") is defined as the angle in the Euclidean plane, given in radians, between the positive x-axis and the ray to the point (x,y) ≠ (0,0).

Function (mathematics) Mathematical binary relation

In mathematics, a function was originally the idealization of how a varying quantity depends on another quantity. For example, the position of a planet is a function of time. Historically, the concept was elaborated with the infinitesimal calculus at the end of the 17th century, and, until the 19th century, the functions that were considered were differentiable. The concept of function was formalized at the end of the 19th century in terms of set theory, and this greatly enlarged the domains of application of the concept.

In mathematics, an argument of a function is a specific input to the function; it is also called an independent variable. When it is clear from the context which argument is meant, the argument may be denoted by the abbreviation arg.

Radian SI derived unit of angle

The radian is the SI unit for measuring angles, and is the standard unit of angular measure used in many areas of mathematics. The length of an arc of a unit circle is numerically equal to the measurement in radians of the angle that it subtends; one radian is just under 57.3 degrees. The unit was formerly an SI supplementary unit, but this category was abolished in 1995 and the radian is now considered an SI derived unit.


The function first appeared in the programming language FORTRAN (in IBM's implementation FORTRAN-IV in 1961) and is defined like this. 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,θ).

Equivalently, is the argument (also called phase or angle) of the complex number

Argument (complex analysis) math function

In mathematics, the argument is a multi-valued function operating on the nonzero complex numbers. With complex number z visualized as a point in the complex plane, the argument of z is the angle between the positive real axis and the line joining the point to the origin, shown as φ in figure 1 and denoted arg z. To define a single-valued function, the principal value of the argument is used. It is chosen to be the unique value of the argument that lies within the interval (–π, π].

Complex number number that can be put in the form a + bi, where a and b are real numbers and i is called the imaginary unit

A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is a solution of the equation x2 = −1. Because no real number satisfies this equation, i is called an imaginary number. For the complex number a + bi, a is called the real part, and b is called the imaginary part. Despite the historical nomenclature "imaginary", complex numbers are regarded in the mathematical sciences as just as "real" as the real numbers, and are fundamental in many aspects of the scientific description of the natural world.

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. [1] 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.

In complex analysis, the principal values of a multivalued function are the values along one chosen branch of that function, so that it is single-valued. The simplest case arises in taking the square root of a positive real number. For example, 4 has two square roots: 2 and –2; of these the positive root, 2, is considered the principal root and is denoted as

In mathematics, a (real) interval is a set of real numbers with the property that any number that lies between two numbers in the set is also included in the set. For example, the set of all numbers x satisfying 0 ≤ x ≤ 1 is an interval which contains 0 and 1, as well as all numbers between them. Other examples of intervals are the set of all real numbers , the set of all negative real numbers, and the empty set.

History and motivation

Graph of the tangent function from -p to +p with the corresponding signs of y over x. The green arrows point to the results of atan2(-1,-1) and atan2(1,1). Atan2 argument sign graph.svg
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 [2] 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 (usable from C#, VB.NET, etc.), the Python math module, the Ruby Math module, the Golang math package [3] , and elsewhere. In addition, many scripting languages, such as Perl, include the C-style atan2(y,x) function.

Programming language language designed to communicate instructions to a machine

A programming language is a formal language, which comprises a set of instructions that produce various kinds of output. Programming languages are used in computer programming to implement algorithms.

C (programming language) general-purpose programming language

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, and therefore it has found lasting use in applications that had formerly been coded in assembly language, including operating systems, as well as various application software for computers ranging from supercomputers to embedded systems.

The C standard library or libc is the standard library for the C programming language, as specified in the ANSI C standard. It was developed at the same time as the C library POSIX specification, which is a superset of it. Since ANSI C was adopted by the International Organization for Standardization, the C standard library is also called the ISO C library.

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 number x+iy. That is, atan2(y, x) = Pr arg(x+iy) = Arg(x+iy). 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 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 (−π/2, π/2), 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 atan2:

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 The principal value of the argument (-atan2- in some circles).svg
The derivation of the principal value of the argument refers to this figure

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

Note that x2 + y2 + 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,

Thus the gradient of atan2 is given by

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.

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.


atan2 for selected rays Atan2 60.svg
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).

Comparison of arctan and atan2 functions Atan2atan.png
Comparison of arctan and atan2 functions

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 . [4]

The two figures below show 3D views of respectively atan2(y, x) and arctan(y/x) 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 arctan(y/x)lines in the X/Y-plane passing through the origin have constant values. For x > 0, the two diagrams give identical values.

Atan2 diagram.svg Atan diagram.svg

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 (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:

This definition is related to the concept of signed zero.

See also

Related Research Articles

Eulers formula e^(ix) = cos(x) + i*sin(x)

Euler's formula, named after Leonhard Euler, is a mathematical formula in complex analysis that establishes the fundamental relationship between the trigonometric functions and the complex exponential function. Euler's formula states that for any real number x:

Polar coordinate system two-dimensional coordinate system in which each point on a plane is determined by a distance from a reference point and an angle from a reference direction

In mathematics, the polar coordinate system is a two-dimensional coordinate system in which each point on a plane is determined by a distance from a reference point and an angle from a reference direction.

Trigonometric functions function of an angle

In mathematics, the trigonometric functions are functions of an angle. They relate the angles of a triangle to the lengths of its sides. Trigonometric functions are important in the study of triangles and modeling periodic phenomena, among many other applications.

Winding number number of times a curve wraps around a point in the plane

In mathematics, the winding number of a closed curve in the plane around a given point is an integer representing the total number of times that curve travels counterclockwise around the point. The winding number depends on the orientation of the curve, and is negative if the curve travels around the point clockwise.

Inverse trigonometric functions inverse function of the trigonometric function

In mathematics, the inverse trigonometric functions are the inverse functions of the trigonometric functions. Specifically, they are the inverses of the sine, cosine, tangent, cotangent, secant, and cosecant functions, and are used to obtain an angle from any of the angle's trigonometric ratios. Inverse trigonometric functions are widely used in engineering, navigation, physics, and geometry.

Clausen function

In mathematics, the Clausen function, introduced by Thomas Clausen (1832), is a transcendental, special function of a single variable. It can variously be expressed in the form of a definite integral, a trigonometric series, and various other special functions. It is intimately connected with the polylogarithm, inverse tangent integral, polygamma function, Riemann zeta function, Dirichlet eta function, and Dirichlet beta function.

In mathematics, the Legendre chi function is a special function whose Taylor series is also a Dirichlet series, given by

In mathematics, the Riemann–Siegel theta function is defined in terms of the Gamma function as

In potential theory, the Poisson kernel is an integral kernel, used for solving the two-dimensional Laplace equation, given Dirichlet boundary conditions on the unit disk. The kernel can be understood as the derivative of the Green's function for the Laplace equation. It is named for Siméon Poisson.

Instantaneous phase

Instantaneous phase and instantaneous frequency are important concepts in signal processing that occur in the context of the representation and analysis of time-varying functions. The instantaneous phase of a complex-valued function s(t), is the real-valued function:

Complex logarithm

In complex analysis, a complex logarithm of the non-zero complex number z, denoted by w = log z, is defined to be any complex number w for which ew = z. This construction is analogous to the real logarithm function ln, which is the inverse of the real exponential function ey, satisfying e lnx = x for positive real numbers x.

Sine ratio between the lengths of the opposite side and the hypotenuse in a right triangle

In mathematics, the sine is a trigonometric function of an angle. The sine of an acute angle is defined in the context of a right triangle: for the specified angle, it is the ratio of the length of the side that is opposite that angle to the length of the longest side of the triangle.

In mathematics, a mean of circular quantities is a mean which is sometimes better-suited for quantities like angles, daytimes, and fractional parts of real numbers. This is necessary since most of the usual means may not be appropriate on circular quantities. For example, the arithmetic mean of 0° and 360° is 180°, which is misleading because for most purposes 360° is the same thing as 0°. As another example, the "average time" between 11 PM and 1 AM is either midnight or noon, depending on whether the two times are part of a single night or part of a single calendar day. This is one of the simplest examples of statistics of non-Euclidean spaces.

Differentiation of trigonometric functions

The differentiation of trigonometric functions is the mathematical process of finding the derivative of a trigonometric function, or its rate of change with respect to a variable. Common trigonometric functions include sin(x), cos(x) and tan(x). For example, the derivative of f(x) = sin(x) is represented as f ′(a) = cos(a). f ′(a) is the rate of change of sin(x) at a particular point a.

Wrapped Cauchy distribution

In probability theory and directional statistics, a wrapped Cauchy distribution is a wrapped probability distribution that results from the "wrapping" of the Cauchy distribution around the unit circle. The Cauchy distribution is sometimes known as a Lorentzian distribution, and the wrapped Cauchy distribution may sometimes be referred to as a wrapped Lorentzian distribution.

The integral of the secant function of trigonometry was the subject of one of the "outstanding open problems of the mid-seventeenth century", solved in 1668 by James Gregory. In 1599, Edward Wright evaluated the integral by numerical methods – what today we would call Riemann sums. He wanted the solution for the purposes of cartography – specifically for constructing an accurate Mercator projection. In the 1640s, Henry Bond, a teacher of navigation, surveying, and other mathematical topics, compared Wright's numerically computed table of values of the integral of the secant with a table of logarithms of the tangent function, and consequently conjectured that

Wrapped exponential distribution

In probability theory and directional statistics, a wrapped exponential distribution is a wrapped probability distribution that results from the "wrapping" of the exponential distribution around the unit circle.


  2. 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).
  3. "src/math/atan2.go - The Go Programming Language". Retrieved 20 April 2018.
  4. "Wolf Jung: Mandel, software for complex dynamics". Retrieved 20 April 2018.
  5. "CLHS: Function ASIN, ACOS, ATAN". LispWorks.
  6. "Microsoft Excel Atan2 Method". Microsoft.
  7. "LibreOffice Calc ATAN2".
  8. "Google Spreadsheets' Function List". Google.
  9. "Numbers' Trigonometric Function List". Apple.
  10. "ANSI SQL:2008 standard". Teradata. Archived from the original on 2015-08-20.
  11. IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.
  12. Burger, Wilhelm; Burge, Mark J. (7 July 2010). "Principles of Digital Image Processing: Fundamental Techniques". Springer Science & Business Media. Retrieved 20 April 2018 via Google Books.
  13. Glisson, Tildon H. (18 February 2011). "Introduction to Circuit Analysis and Design". Springer Science & Business Media. Retrieved 20 April 2018 via Google Books.
Other implementations/code for atan2