atan2

It was originally intended to return a correct and unambiguous value for the angle ⁠

⁠ (a half turn) must be added to place the point in the correct quadrant.

function does away with this correction, simplifying code and mathematical formulas.

The ordinary single-argument arctangent function only returns angle measures in the interval ⁠

⁠, and when invoking it to find the direction angle from the origin to an arbitrary point ⁠

⁠ in the Cartesian plane, it will yield incorrect results when the point is in the left half-plane ⁠

To fully determine the direction angle from the origin given a point ⁠

⁠ using the arctangent function, mathematical formulas or computer code must handle multiple cases; at least one for positive values of

Finding angle measures and converting Cartesian to polar coordinates are common in scientific computing, and this code is redundant and error-prone.

⁠ are used to determine the quadrant of the result and select the correct branch of the multivalued function ⁠

The atan2 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 atan2 function is now included in many other programming languages, and is also commonly found in mathematical formulas throughout science and engineering.

However, this is the opposite of the conventional component order for complex numbers,

The function atan2 computes the principal argument of the complex number ⁠

⁠, which is also the imaginary part of the principal value of the complex logarithm.

⁠ (corresponding to complete turns around the origin) gives another argument of the same complex number, but the principal argument is defined as the unique representative angle in the interval ⁠

At points where these derivatives exist, atan2 is, except for a constant, equal to arctan(y/x).

Hence, 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.

This figure shows values of atan2 along selected rays from the origin, labelled at the unit 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).

[3] The two figures below show 3D views of respectively atan2(y, x) and arctan(⁠y/x⁠) over a region of the plane.

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;[4] the solar azimuth angle can be calculated similarly with the east- and north-components of the solar vector as its arguments.

[5] These different conventions can be realized by swapping the positions and changing the signs of the x- and y-arguments as follows: As an example, let

Changing the sign of the x- and/or y-arguments and/or 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.

atan2( y , x ) returns the angle θ between the positive x -axis and the ray from the origin to the point ( x , y ) , confined to (− π , π ] .
Graph of over
Graph of the tangent function from −π to with the corresponding signs of y / x . The green arrows point to the results of atan2(−1, −1) and atan2(1, 1) .
The tangent of half an angle can be computed in terms of coordinates x and y and radius r .
atan2 for selected rays
Comparison of arctan and atan2 functions