Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Motivation  



1.1  Argument order  







2 Definition and computation  





3 Derivative  





4 Illustrations  





5 Angle sum and difference identity  





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





7 Realizations of the function in common computer languages  





8 See also  





9 References  





10 External links  





11 Notes  














atan2






العربية
Čeština
Deutsch
Español
فارسی
Français
Italiano
Magyar



 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 




In other projects  



Wikimedia Commons
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


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

Incomputing and mathematics, the function atan2 is the 2-argument arctangent. By definition, is the angle measure (inradians, with ) between the positive -axis and the ray from the origin to the point in the Cartesian plane. Equivalently, is the argument (also called phaseorangle) of the complex number

The 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)topolar coordinates (r, θ). If and , then and

Ifx > 0, the desired angle measure is However, when x < 0, the angle isdiametrically opposite the desired angle, and ±π (a half turn) must be added to place the point in the correct quadrant.[1] Using the function does away with this correction, simplifying code and mathematical formulas.

Motivation

[edit]
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 ordinary single-argument arctangent function only returns angle measures in the interval and when invoking it to find the angle measure between the x-axis and an arbitrary vector in the Cartesian plane, there is no simple way to indicate a direction in the left half-plane (that is, a point with ). Diametrically opposite angle measures have the same tangent because so the tangent is not in itself sufficient to uniquely specify an angle.

To determine an angle measure using the arctangent function given a point or vector mathematical formulas or computer code must handle multiple cases; at least one for positive values of and one for negative values of and sometimes additional cases when is negative or one coordinate is zero. Finding angle measures and converting Cartesian to polar coordinates are common in scientific computing, and this code is redundant and error-prone.

To remedy this, computer programming languages introduced the atan2 function, at least as early as the Fortran IV language of the 1960s.[2] 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 signsofx 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 matrixtoEuler 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

[edit]

In 1961, Fortran introduced the atan2 function with argument order so that the argument (phase angle) of a complex number is This follows the left-to-right order of a fraction written so that for positive values of However, this is the opposite of the conventional component order for complex numbers, or as coordinates 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 OpenOffice Calc uses and Mathematica uses defaulting to one-argument arctangent if called with one argument.

Definition and computation

[edit]

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 to define a surface that has no discontinuities except along the semi-infinite line x<0 y=0:

A compact expression with four overlapping half-planes is

The Iverson bracket notation allows for an even more compact expression:[note 1]

Formula without apparent conditional construct:

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

Notes:

Derivative

[edit]

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 > 0ory ≠ 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.

Illustrations

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

This figure shows the values of along 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 .[3]

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.

Angle sum and difference identity

[edit]

The sum or difference of multiple angles to be computed by can alternately be computed by composing them as complex numbers. Given two coordinate pairs and , their angles from the positive axis will be composed (and lengths multiplied) if they are treated as complex numbers and then multiplied together, . The resulting angle can be found using a single operation, so long as the as long as the resulting angle lies in :

and likewise for more than two coordinate pairs. If the composed angle crosses the negative -axis (i.e. exceeds the range ), then the crossings can be counted and the appropriate integer multiple of 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 .

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

[edit]

The 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. 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.[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 and , then the east-counterclockwise format gives , the north-clockwise format gives , and the south-clockwise format gives .

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.

Realizations of the function in common computer languages

[edit]

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

The convention is used by:

This definition is related to the concept of signed zero.

See also

[edit]

References

[edit]
  1. ^ "The argument of a complex number" (PDF). Santa Cruz Institute for Particle Physics. Winter 2011.
  • ^ 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).
  • ^ "Wolf Jung: Mandel, software for complex dynamics". www.mndynamics.com. Retrieved 20 April 2018.
  • ^ "Wind Direction Quick Reference". NCAR UCAR Earth Observing Laboratory.
  • ^ Zhang, Taiping; Stackhouse, Paul W.; MacPherson, Bradley; Mikovitz, J. Colleen (2021). "A solar azimuth formula that renders circumstantial treatment unnecessary without compromising mathematical rigor: Mathematical setup, application and extension of a formula based on the subsolar point and atan2 function". Renewable Energy. 172: 1333–1340. Bibcode:2021REne..172.1333Z. doi:10.1016/j.renene.2021.03.047. S2CID 233631040.
  • ^ "Microsoft Excel Atan2 Method". Microsoft. 14 June 2014.
  • ^ "LibreOffice Calc ATAN2". Libreoffice.org.
  • ^ "Functions and formulas – Docs Editors Help". support.google.com.
  • ^ "Numbers' Trigonometric Function List". Apple.
  • ^ "CLHS: Function ASIN, ACOS, ATAN". LispWorks.
  • ^ "Mathematics · The Julia Language". docs.julialang.org.
  • ^ "Frequently Asked Questions · The Julia Language". docs.julialang.org.
  • ^ IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.
  • ^ Burger, Wilhelm; Burge, Mark J. (7 July 2010). Principles of Digital Image Processing: Fundamental Techniques. Springer Science & Business Media. ISBN 978-1-84800-191-6. Retrieved 20 April 2018 – via Google Books.
  • ^ Glisson, Tildon H. (18 February 2011). Introduction to Circuit Analysis and Design. Springer Science & Business Media. ISBN 9789048194438. Retrieved 20 April 2018 – via Google Books.
  • [edit]
    Other implementations/code for atan2

    Notes

    [edit]
    1. ^ Assuming the definitions and for any
  • ^ One can apply the periodicity of the result to map to another desired range, e.g. mapping to [0, 2π) by adding to the negative results.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Atan2&oldid=1234725088"

    Category: 
    Inverse trigonometric functions
    Hidden categories: 
    Articles with short description
    Short description matches Wikidata
     



    This page was last edited on 15 July 2024, at 20:37 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki