Lably
Skip to content

Atan2

Arctangent function with two arguments

  • 10 min read
  • 7 chapters

Related on Lably

Through Computing, Mathematics and Function (mathematics)

The atan2 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 ⁠(x,y)⁠ to polar coordinates ⁠(r,θ)⁠. If θ=atan2⁡(y,x) and r=x2+y2, then x=rcos⁡θ and ⁠y=rsin⁡θ⁠.

If ⁠x>0⁠, the desired angle measure is ⁠θ=atan2⁡(y,x)=arctan⁡(y/x)⁠. However, when x < 0, the angle arctan⁡(y/x) is diametrically opposite the desired angle, and ⁠±π⁠ (a half turn) must be added to place the point in the correct quadrant. Using the atan2 function does away with this correction, simplifying code and mathematical formulas.

Motivation

It is common in practical applications of geometry to convert between rectangular coordinates ⁠(x,y)⁠ and polar coordinates ⁠(r,θ)⁠ for representing points in the Euclidean plane, for example to find the signed angle representing the orientation of a vector or to calculate the argument of a complex number. The relationships between rectangular and polar coordinates can be summarized by the equations: x=rcos⁡θ,r=x2+y2,y=rsin⁡θ,tan⁡θ=yx, where ⁠sin⁠, ⁠cos⁠, and ⁠tan⁠ are trigonometric functions and ⁠x2+y2⁠ is the Pythagorean sum of ⁠x⁠ and ⁠y⁠, i.e. the hypotenuse of a right triangle with legs ⁠x⁠ and ⁠y⁠.

Computing ⁠x⁠ and ⁠y⁠ from ⁠r⁠ and ⁠θ⁠ directly from these formulas is straightforward. In the other direction, though, it is helpful to provide special functions to implement the conversions. The formula to compute ⁠r⁠ from ⁠x⁠ and ⁠y⁠ can lead to numerical overflows and underflows, avoided by the hypot function of many standard libraries. The computation of ⁠θ⁠ from ⁠x⁠ and ⁠y⁠ is more complicated, for two reasons: (1) because it could involve a division by zero and (2) because the quotient ⁠y/x⁠ is equal to ⁠(−y)/(−x)⁠, and therefore does not distinguish between point ⁠(x,y)⁠ and the opposite point ⁠(−x,−y)⁠. The ordinary single-argument arctangent function only returns angle measures in the interval ⁠(−12π,12π)⁠, so a naïve implementation of a conversion from rectangular to polar coordinates, ⁠θ=arctan⁡(y/x)⁠, will yield incorrect results whenever ⁠x≤0⁠.

To fully determine the direction angle from the origin given a point ⁠(x,y)⁠ using the arctangent function, mathematical formulas or computer code must handle multiple cases; at least one for positive values of x and one for negative values of ⁠x⁠, and sometimes additional cases when ⁠y⁠ is negative or one coordinate is zero.

To save programmers the trouble, computer programming languages introduced the atan2 function, at least as early as the Fortran IV language of the 1960s. The quantity ⁠atan2⁡(y,x)⁠ is the angle measure between the ⁠x⁠-axis and a ray from the origin to a point ⁠(x,y)⁠ anywhere in the Cartesian plane. The signs of ⁠x⁠ and ⁠y⁠ are used to determine the quadrant of the result and select the correct branch of the multivalued function ⁠Arctan⁡(y/x)⁠.

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.

Argument order

In 1961, Fortran introduced the atan2 function with argument order ⁠(y,x)⁠ so that the argument (phase angle) of a complex number is ⁠arg⁡z=atan2⁡(Im⁡z,Re⁡z)⁠. This follows the left-to-right order of a fraction written ⁠y/x⁠, so that atan2⁡(y,x)=atan⁡(y/x) for positive values of ⁠x⁠. However, this is the opposite of the conventional component order for complex numbers, ⁠z=x+iy⁠, or as coordinates ⁠(Re⁡z,Im⁡z)⁠. 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 ⁠Atan2⁡(x,y)⁠, OpenOffice Calc uses ⁠arctan2⁡(x,y)⁠, and Mathematica uses ⁠ArcTan⁡[x,y]⁠, defaulting to one-argument arctangent if called with one argument.

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Topics in mathematics