Atan2

From testwiki
Jump to navigation Jump to search

Template:Short description Template:Lowercase title

Template:Math returns the angle Template:Mvar between the positive Template:Mvar-axis and the ray from the origin to the point Template:Math, confined to Template:Open-closed.
Graph of atan2⁡(y,x) over y/x

In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition, θ=atan2⁡(y,x) is the angle measure (in radians, with −π<θ≤π) between the positive x-axis and the ray from the origin to the point (x,y) in the Cartesian plane. Equivalently, atan2⁡(y,x) is the argument (also called phase or angle) of the complex number x+iy. (The argument of a function and the argument of a complex number, each mentioned above, should not be confused.)

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 Template:Tmath in converting from Cartesian coordinates Template:Tmath to polar coordinates Template:Tmath. If θ=atan2⁡(y,x) and r=x2+y2, then x=rcos⁡θ and y=rsin⁡θ.

If Template:Tmath, the desired angle measure is θ=atan2⁡(y,x)=arctan⁡(y/x). However, when Template:Math, the angle arctan⁡(y/x) is diametrically opposite the desired angle, and Template:Tmath (a half turn) must be added to place the point in the correct quadrant.[1] 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 Template:Tmath and polar coordinates Template:Tmath 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 Template:Tmath, Template:Tmath, and Template:Tmath are trigonometric functions and Template:Tmath is the Pythagorean sum of Template:Tmath and Template:Tmath, i.e. the hypotenuse of a right triangle with legs Template:Tmath and Template:Tmath.

Graph of the tangent function from Template:Math to Template:Math with the corresponding signs of Template:Math. The green arrows point to the results of Template:Math and Template:Math.

Computing Template:Tmath and Template:Tmath from Template:Tmath and Template:Tmath 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 Template:Tmath from Template:Tmath and Template:Tmath can lead to numerical overflows and underflows, avoided by the hypot function of many standard libraries. The computation of Template:Tmath from Template:Tmath and Template:Tmath is more complicated, for two reasons: (1) because it could involve a division by zero and (2) because the quotient Template:Tmath is equal to Template:Tmath, and therefore does not distinguish between point Template:Tmath and the opposite point Template:Tmath. The ordinary single-argument arctangent function only returns angle measures in the interval Template:Tmath, so a naïve implementation of a conversion from rectangular to polar coordinates, Template:Tmath, will yield incorrect results whenever Template:Tmath.

To fully determine the direction angle from the origin given a point Template:Tmath 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 Template:Tmath, and sometimes additional cases when Template:Tmath is negative or one coordinate is zero.

To save programmers the trouble, computer programming languages introduced the Template:Math function, at least as early as the Fortran IV language of the 1960s.[2] The quantity Template:Tmath is the angle measure between the Template:Tmath-axis and a ray from the origin to a point Template:Tmath anywhere in the Cartesian plane. The signs of Template:Tmath and Template:Tmath are used to determine the quadrant of the result and select the correct branch of the multivalued function Template:Tmath.

The Template:Math 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 Template:Math 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 Template:Math function with argument order Template:Tmath 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.

Definition and computation

The function Template:Math computes the principal argument of the complex number Template:Tmath, which is also the imaginary part of the principal value of the complex logarithm. That is, atan2⁡(y,x)=arg⁡(x+iy)=Im⁡log⁡(x+iy).

Adding any integer multiple of Template:Tmath (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 Template:Tmath.

In terms of the standard arctangent function, whose image is Template:Tmath, Template:Math can be expressed piecewise: atan2⁡(y,x)={arctan⁡(yx)if x>0,arctan⁡(yx)+πif x<0 and y≥0,arctan⁡(yx)−πif x<0 and y<0,+π2if x=0 and y>0,−π2if x=0 and y<0,undefinedif x=0 and y=0.

The tangent of half an angle can be computed in terms of coordinates Template:Mvar and Template:Mvar and radius Template:Mvar.

In cases where the Template:Math function is unavailable or inconvenient to use, and where reducing the number of cases that must be considered is important (e.g. for vectorized computations on a GPU), it can be useful to compute Template:Math through a tangent half-angle formula:[3]

atan2⁡(y,x)=2arctan⁡(yx2+y2+x), except in the case that Template:Tmath and Template:Tmath, when by convention atan2 is equal to Template:Tmath.

Derivative

Template:Details As the function Template:Math is a function of two variables, it has two partial derivatives. At points where these derivatives exist, Template:Math is, except for a constant, equal to Template:Math. Hence, ∂∂xatan2⁡(y,x)=∂∂xarctan⁡(yx)=−yx2+y2,∂∂yatan2⁡(y,x)=∂∂yarctan⁡(yx)=xx2+y2. These two partial derivatives are the coordinates of the gradient.

Informally representing the function Template:Math as the angle function Template:Math (which is only defined up to a constant) yields the following formula for the total differential: dθ=∂∂xatan2⁡(y,x)dx+∂∂yatan2⁡(y,x)dy=−yx2+y2dx+xx2+y2dy.

While the function Template:Math is discontinuous along the negative Template:Mvar-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 Template:Math do not contain trigonometric functions, making it particularly useful in many applications (e.g. embedded systems) where trigonometric functions can be expensive to evaluate.

Illustrations

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 Template:Math computes the angle corresponding to the point Template:Math. Template:Clear

Comparison of arctan and atan2 functions

This figure shows the values of arctan⁡(tan⁡(θ)) along with atan2⁡(sin⁡(θ),cos⁡(θ)) for 0≤θ≤2π. Both functions are odd and periodic with periods π and 2π, respectively, and thus can easily be supplemented to any region of real values of θ. One can clearly see the branch cuts of the atan2-function at θ=π, and of the arctan-function at θ∈{π2,3π2}.[4]

The two figures below show 3D views of respectively Template:Math and Template:Math over a region of the plane. Note that for Template:Math, rays in the Template:Mvar/Template:Mvar-plane emanating from the origin have constant values, but for Template:Math lines in the Template:Mvar/Template:Mvar-plane passing through the origin have constant values. For Template:Math, the two diagrams give identical values.

Angle sum and difference identity

Template:Main

The sum or difference of multiple angles to be computed by Template:Tmath can alternately be computed by composing them as complex numbers. Given two coordinate pairs Template:Tmath and Template:Tmath, their angles from the positive Template:Tmath axis will be composed (and lengths multiplied) if they are treated as complex numbers and then multiplied together, Template:TmathTemplate:Tmath. The resulting angle can be found using a single Template:Tmath operation, so long as the resulting angle lies in Template:Tmath: atan2⁡(y1,x1)±atan2⁡(y2,x2)=atan2⁡(y1x2±x1y2,x1x2∓y1y2), and likewise for more than two coordinate pairs. If the composed angle crosses the negative Template:Tmath-axis (i.e. exceeds the range Template:Tmath), then the crossings can be counted and the appropriate integer multiple of Template:Tmath added to the final result to correct it.

This difference formula is frequently used in practice to compute the angle between two planar vectors, since the resulting angle is always in the range Template:Tmath.

East-counterclockwise, north-clockwise and south-clockwise conventions, etc.

The atan2 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 atan2 function with the east- and north-components of the wind vector as its arguments;[5] the solar azimuth angle can be calculated similarly with the east- and north-components of the solar vector as its arguments. The wind direction is normally defined in the north-clockwise sense, and the solar azimuth angle uses both the north-clockwise and south-clockwise conventions widely.[6] These different conventions can be realized by swapping the positions and changing the signs of the Template:Mvar- and Template:Mvar-arguments as follows:

  • atan2⁡(y,x), (East-Counterclockwise Convention)
  • atan2⁡(x,y), (North-Clockwise Convention)
  • atan2⁡(−x,−y). (South-Clockwise Convention)

As an example, let x0=32 and y0=12, then the east-counterclockwise format gives atan2⁡(y0,x0)⋅180∘π=30∘, the north-clockwise format gives atan2⁡(x0,y0)⋅180∘π=60∘, and the south-clockwise format gives atan2⁡(−x0,−y0)⋅180∘π=−120∘.

Changing the sign of the Template:Mvar- and/or Template:Mvar-arguments and/or swapping their positions can create 8 possible variations of the atan2 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.

Realizations of the function in common computer languages

The realization of the function differs from one computer language to another.

The (Re⁡,Im⁡) convention (reversed relative to the convention used in the discussion above) is used by:

  • In Microsoft Excel,[7] OpenOffice.org Calc, LibreOffice Calc,[8] Google Spreadsheets,[9] and iWork Numbers,[10] the 2-argument arctangent function has the two arguments in the standard sequence (Re⁡,Im⁡).
  • 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.
  • On most TI graphing calculators (excluding the TI-85 and TI-86), the equivalent function is called R►Pθ and has the arguments (Re⁡,Im⁡).
  • On TI-85 the Template:Math 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: Template:Math.

The (Im⁡,Re⁡) convention is used by:

  • 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 Template:Closed-closed rather than raising an error or returning a NaN (Not a Number).
  • In Common Lisp, where optional arguments exist, the atan function allows one to optionally supply the Template:Mvar coordinate: (atan y x).[11]
  • In Julia, the situation is similar to Common Lisp: instead of atan2, the language has a one-parameter and a two-parameter form for atan.[12] However, it has many more than two methods, to allow for aggressive optimisation at compile time.[13]
  • Mathcad uses the argument order atan2(x, y). atan2(0, 0) is undefined.[14]
  • For systems implementing signed zero, infinities, or Not a Number (for example, IEEE floating point), it is common to implement reasonable extensions that may extend the range of values produced to include Template:Math and −0 when Template:Math. These also may return NaN or raise an exception when given a NaN argument.
  • In the Intel x86 Architecture assembler code, atan2 is known as the FPATAN (floating-point partial arctangent) instruction.[15] It can deal with infinities and results lie in the closed interval Template:Closed-closed, e.g. atan2(∞, x) = Template:Math for finite Template:Mvar. Particularly, FPATAN is defined when both arguments are zero:Template:Bulleted listTemplate:PbThis definition is related to the concept of signed zero.
  • In mathematical writings other than source code, such as in books and articles, the notations Arctan[16] and Tan−1[17] have been utilized; these are capitalized variants of the regular arctan and tan−1. This usage is consistent with the complex argument notation, such that Template:Math.
  • On HP calculators, treat the coordinates as a complex number and then take the ARG. Or << C->R ARG >> 'ATAN2' STO.
  • On scientific calculators, the function can often be calculated as the angle given when Template:Math is converted from rectangular coordinates to polar coordinates.
  • Systems supporting symbolic mathematics normally return an undefined value for Template:Math or otherwise signal that an abnormal condition has arisen.
  • 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 Template:Math can be implemented in a numerically reliable manner by the CORDIC method. Thus, implementations of Template:Math will probably choose to compute Template:Math.

See also

References

Template:Reflist

Other implementations/code for atan2

Notes


Template:Trigonometric and hyperbolic functions