fbpx
Wikipedia

atan2

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

atan2(y, x) returns the angle θ between the ray to the point (x, y) and the positive x-axis, confined to (−π, π].
Graph of over

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

If x > 0, the desired angle measure is However, when x < 0, the angle is diametrically 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

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

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:

  • This produces results in the range (−π, π].[note 2]
  • As mentioned above, the principal value of the argument atan2(y, x) can be related to arctan(y/x) by trigonometry. The derivation goes as follows:
    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.

Derivative

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.

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

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.

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

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:

  •   (East-Counterclockwise Convention)
  •   (North-Clockwise Convention)
  •  . (South-Clockwise Convention)

As an example, let   and  , then the east-counterclockwise format gives  , the north-clockwise format gives  , and the south-clockwise format gives  .

Apparently, changing the sign of the x- and/or y-arguments and 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

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

  • In Microsoft Excel,[6] OpenOffice.org Calc, LibreOffice Calc,[7] Google Spreadsheets,[8] iWork Numbers,[9] and ANSI SQL:2008 standard,[10] the 2-argument arctangent function has the two arguments in the standard sequence   (reversed relative to the convention used in the discussion above).
  • 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  .
  • On TI-85 the arg 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: x + iy = (x, y).

The   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 [−π, π] 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 x 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 (see the section "Why don't you compile Matlab/Python/R/… code to Julia?" [13]).
  • For systems implementing signed zero, infinities, or Not a Number (for example, IEEE floating point), it is common to implement reasonable extensions which may extend the range of values produced to include −π and −0 when y = −0. 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.[14] It can deal with infinities and results lie in the closed interval [−π, π], e.g. atan2(∞, x) = +π/2 for finite x. Particularly, FPATAN is defined when both arguments are zero:
    atan2(+0, +0) = +0;
    atan2(+0, −0) = +π;
    atan2(−0, +0) = −0;
    atan2(−0, −0) = −π.
This 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[15] and Tan−1[16] 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 Atan(y, x) = Arg(x + iy).
  • 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 (x, y) is converted from rectangular coordinates to polar coordinates.
  • Systems supporting symbolic mathematics normally return an undefined value for atan2(0, 0) 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 atan2 can be implemented in a numerically reliable manner by the CORDIC method. Thus implementations of atan(y) will probably choose to compute atan2(y, 1).

See also

References

  1. ^ "The argument of a complex number" (PDF). Santa Cruz Institute for Particle Physics. Winter 2011.
  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. ^ "Wolf Jung: Mandel, software for complex dynamics". www.mndynamics.com. Retrieved 20 April 2018.
  4. ^ "Wind Direction Quick Reference". NCAR UCAR Earth Observing Laboratory.
  5. ^ 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. doi:10.1016/j.renene.2021.03.047. S2CID 233631040.
  6. ^ "Microsoft Excel Atan2 Method". Microsoft.
  7. ^ "LibreOffice Calc ATAN2". Libreoffice.org.
  8. ^ "Functions and formulas – Docs Editors Help". support.google.com.
  9. ^ "Numbers' Trigonometric Function List". Apple.
  10. ^ . Teradata. Archived from the original on 2015-08-20.
  11. ^ "CLHS: Function ASIN, ACOS, ATAN". LispWorks.
  12. ^ "Mathematics · The Julia Language". docs.julialang.org.
  13. ^ "Frequently Asked Questions · The Julia Language". docs.julialang.org.
  14. ^ IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.
  15. ^ 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.
  16. ^ 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.

External links

  • ATAN2 Online calculator
  • Java 1.6 SE JavaDoc
  • atan2 at Everything2
  • PicBasic Pro solution atan2 for a PIC18F
Other implementations/code for atan2
  • . Archived from the original on 18 November 2020. Retrieved 21 February 2022.
  • . Archived from the original on 18 October 2018. Retrieved 21 February 2022.
  • . Archived from the original on 6 September 2017. Retrieved 21 February 2022.

Notes

  1. ^ Assuming the definitions     and   for any  
  2. ^ 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.

atan2, computing, mathematics, function, argument, arctangent, definition, displaystyle, theta, operatorname, angle, measure, radians, with, displaystyle, theta, between, positive, displaystyle, axis, from, origin, point, displaystyle, cartesian, plane, equiva. In computing and mathematics the function atan2 is the 2 argument arctangent By definition 8 atan2 y x displaystyle theta operatorname atan2 y x is the angle measure in radians with p lt 8 p displaystyle pi lt theta leq pi between the positive x displaystyle x axis and the ray from the origin to the point x y displaystyle x y in the Cartesian plane Equivalently atan2 y x displaystyle operatorname atan2 y x is the argument also called phase or angle of the complex number x i y displaystyle x iy atan2 y x returns the angle 8 between the ray to the point x y and the positive x axis confined to p p Graph of atan2 y x displaystyle operatorname atan2 y x over y x displaystyle y x The atan2 displaystyle operatorname 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 8 in converting from Cartesian coordinates x y to polar coordinates r 8 If 8 atan2 y x displaystyle theta operatorname atan2 y x and r x 2 y 2 textstyle r sqrt x 2 y 2 then x r cos 8 displaystyle x r cos theta and y r sin 8 displaystyle y r sin theta If x gt 0 the desired angle measure is 8 atan2 y x arctan y x textstyle theta operatorname atan2 y x arctan left y x right However when x lt 0 the angle arctan y x displaystyle arctan y x is diametrically opposite the desired angle and p a half turn must be added to place the point in the correct quadrant 1 Using the atan2 displaystyle operatorname atan2 function does away with this correction simplifying code and mathematical formulas Contents 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 NotesMotivation Edit Graph of the tangent function from p to p 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 1 2 p 1 2 p displaystyle left tfrac 1 2 pi tfrac 1 2 pi right 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 x y displaystyle x y with x lt 0 displaystyle x lt 0 Diametrically opposite angle measures have the same tangent because y x y x displaystyle y x y x so the tangent y x displaystyle y x is not in itself sufficient to uniquely specify an angle To determine an angle measure using the arctangent function given a point or vector x y displaystyle x y mathematical formulas or computer code must handle multiple cases at least one for positive values of x displaystyle x and one for negative values of x displaystyle x and sometimes additional cases when y displaystyle y 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 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 Edit In 1961 Fortran introduced the atan2 function with argument order y x displaystyle y x so that the argument phase angle of a complex number is arg z atan2 Im z Re z displaystyle operatorname arg z operatorname atan2 operatorname Im z operatorname Re z This follows the left to right order of a fraction written y x displaystyle y x so that atan2 y x atan y x displaystyle operatorname atan2 y x operatorname atan y x for positive values of x displaystyle x However this is the opposite of the conventional component order for complex numbers z x i y displaystyle z x iy or as coordinates Re z Im z displaystyle operatorname Re z operatorname 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 displaystyle operatorname Atan2 x y OpenOffice Calc uses arctan2 x y displaystyle operatorname arctan2 x y and Mathematica uses ArcTan x y displaystyle operatorname ArcTan x y defaulting to one argument arctangent if called with one argument Definition and computation EditThe function atan2 computes the principal value of the argument function applied to the complex number x i y That is atan2 y x Pr arg x i y Arg x i y The argument could be changed by an arbitrary multiple of 2p 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 p p displaystyle pi pi that is p lt atan2 y x p In terms of the standard arctan function whose range is p 2 p 2 it can be expressed as follows to define a surface that has no discontinuities except along the semi infinite line x lt 0 y 0 atan2 y x arctan y x if x gt 0 arctan y x p if x lt 0 and y 0 arctan y x p if x lt 0 and y lt 0 p 2 if x 0 and y gt 0 p 2 if x 0 and y lt 0 undefined if x 0 and y 0 displaystyle operatorname atan2 y x begin cases arctan left frac y x right amp text if x gt 0 5mu arctan left frac y x right pi amp text if x lt 0 text and y geq 0 5mu arctan left frac y x right pi amp text if x lt 0 text and y lt 0 5mu frac pi 2 amp text if x 0 text and y gt 0 5mu frac pi 2 amp text if x 0 text and y lt 0 5mu text undefined amp text if x 0 text and y 0 end cases A compact expression with four overlapping half planes isatan2 y x arctan y x if x gt 0 p 2 arctan x y if y gt 0 p 2 arctan x y if y lt 0 arctan y x p if x lt 0 undefined if x 0 and y 0 displaystyle operatorname atan2 y x begin cases arctan left frac y x right amp text if x gt 0 5mu frac pi 2 arctan bigl frac x y bigr amp text if y gt 0 5mu frac pi 2 arctan bigl frac x y bigr amp text if y lt 0 5mu arctan left frac y x right pm pi amp text if x lt 0 5mu text undefined amp text if x 0 text and y 0 end cases The Iverson bracket notation allows for an even more compact expression note 1 atan2 y x arctan y x x 0 1 2 y lt 0 p x lt 0 1 2 p x 0 undefined x 0 y 0 displaystyle begin aligned operatorname atan2 y x amp arctan left frac y x right x neq 0 5mu amp qquad bigl 1 2 y lt 0 bigr left pi x lt 0 tfrac 1 2 pi x 0 right 5mu amp qquad text undefined x 0 wedge y 0 end aligned Formula without apparent conditional construct atan2 y x lim z x arctan y z p 2 sgn y sgn x sgn x 1 displaystyle operatorname atan2 y x lim z to x arctan left frac y z right frac pi 2 operatorname sgn y operatorname sgn x left operatorname sgn x 1 right The following expression derived from the tangent half angle formula can also be used to define atan2 atan2 y x 2 arctan y x 2 y 2 x if x gt 0 or y 0 p if x lt 0 and y 0 undefined if x 0 and y 0 displaystyle operatorname atan2 y x begin cases 2 arctan left frac y sqrt x 2 y 2 x right amp text if x gt 0 text or y neq 0 pi amp text if x lt 0 text and y 0 text undefined amp text if x 0 text and y 0 end cases 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 x 2 y 2 textstyle sqrt x 2 y 2 expand near the region x lt 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 atan2 y x 2 arctan y x 2 y 2 x if x gt 0 2 arctan x 2 y 2 x y if x 0 and y 0 p if x lt 0 and y 0 undefined if x 0 and y 0 displaystyle operatorname atan2 y x begin cases 2 arctan left frac y sqrt x 2 y 2 x right amp text if x gt 0 2 arctan left frac sqrt x 2 y 2 x y right amp text if x leq 0 text and y neq 0 pi amp text if x lt 0 text and y 0 text undefined amp text if x 0 text and y 0 end cases The derivation of the principal value of the argument refers to this figure Notes This produces results in the range p p note 2 As mentioned above the principal value of the argument atan2 y x can be related to arctan y x by trigonometry The derivation goes as follows If x y r cos 8 r sin 8 then tan 8 2 y r x It follows that atan2 y x 8 2 8 2 2 arctan y x 2 y 2 x displaystyle operatorname atan2 y x theta 2 theta 2 2 arctan frac y sqrt x 2 y 2 x Note that x2 y2 x 0 in the domain in question Derivative EditFurther information Winding number 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 gt 0 or y 0 x atan2 y x x arctan y x y x 2 y 2 y atan2 y x y arctan y x x x 2 y 2 displaystyle begin aligned amp frac partial partial x operatorname atan2 y x frac partial partial x arctan left frac y x right frac y x 2 y 2 5pt amp frac partial partial y operatorname atan2 y x frac partial partial y arctan left frac y x right frac x x 2 y 2 end aligned Thus the gradient of atan2 is given by atan2 y x y x 2 y 2 x x 2 y 2 displaystyle nabla text atan2 y x left y over x 2 y 2 x over x 2 y 2 right Informally representing the function atan2 as the angle function 8 x y atan2 y x which is only defined up to a constant yields the following formula for the total differential d 8 x atan2 y x d x y atan2 y x d y y x 2 y 2 d x x x 2 y 2 d y displaystyle begin aligned mathrm d theta amp frac partial partial x operatorname atan2 y x mathrm d x frac partial partial y operatorname atan2 y x mathrm d y 5pt amp frac y x 2 y 2 mathrm d x frac x x 2 y 2 mathrm d y end aligned 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 arctan tan 8 displaystyle arctan tan theta along with atan2 sin 8 cos 8 displaystyle operatorname atan2 sin theta cos theta for 0 8 2 p displaystyle 0 leq theta leq 2 pi Both functions are odd and periodic with periods p displaystyle pi and 2 p displaystyle 2 pi respectively and thus can easily be supplemented to any region of real values of 8 displaystyle theta One can clearly see the branch cuts of the atan2 displaystyle operatorname atan2 function at 8 p displaystyle theta pi and of the arctan displaystyle arctan function at 8 p 2 3 p 2 displaystyle theta in tfrac pi 2 tfrac 3 pi 2 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 gt 0 the two diagrams give identical values Angle sum and difference identity EditMain article List of trigonometric identities Angle sum and difference identities Sums of atan2 displaystyle operatorname atan2 may be collapsed into a single operation according to the following identity atan2 y 1 x 1 atan2 y 2 x 2 atan2 y 1 x 2 y 2 x 1 x 1 x 2 y 1 y 2 displaystyle operatorname atan2 y 1 x 1 pm operatorname atan2 y 2 x 2 operatorname atan2 y 1 x 2 pm y 2 x 1 x 1 x 2 mp y 1 y 2 provided that atan2 y 1 x 1 atan2 y 2 x 2 p p displaystyle operatorname atan2 y 1 x 1 pm operatorname atan2 y 2 x 2 in pi pi The proof involves considering two cases one where y 2 0 displaystyle y 2 neq 0 or x 2 gt 0 displaystyle x 2 gt 0 and one where y 2 0 displaystyle y 2 0 and x 2 lt 0 displaystyle x 2 lt 0 We only consider the case where y 2 0 displaystyle y 2 neq 0 or x 2 gt 0 displaystyle x 2 gt 0 To start we make the following observations atan2 y x atan2 y x displaystyle operatorname atan2 y x operatorname atan2 y x provided that y 0 displaystyle y neq 0 or x gt 0 displaystyle x gt 0 Arg x i y atan2 y x displaystyle operatorname Arg x iy operatorname atan2 y x where Arg displaystyle operatorname Arg is the complex argument function 8 Arg e i 8 displaystyle theta operatorname Arg e i theta whenever 8 p p displaystyle theta in pi pi a consequence of Euler s formula Arg e i Arg z 1 e i Arg z 2 Arg z 1 z 2 displaystyle operatorname Arg e i operatorname Arg zeta 1 e i operatorname Arg zeta 2 operatorname Arg zeta 1 zeta 2 To see 4 we have the identity e i Arg z z displaystyle e i operatorname Arg zeta bar zeta where z z z displaystyle bar zeta zeta left zeta right hence Arg e i Arg z 1 e i Arg z 2 Arg z 1 z 2 displaystyle operatorname Arg e i operatorname Arg zeta 1 e i operatorname Arg zeta 2 operatorname Arg bar zeta 1 bar zeta 2 Furthermore since Arg z Arg a z displaystyle operatorname Arg zeta operatorname Arg a zeta for any positive real value a displaystyle a then if we let z z 1 z 2 displaystyle zeta zeta 1 zeta 2 and a 1 z 1 z 2 displaystyle a frac 1 left zeta 1 right left zeta 2 right then we have Arg z 1 z 2 Arg z 1 z 2 displaystyle operatorname Arg bar zeta 1 bar zeta 2 operatorname Arg zeta 1 zeta 2 From these observations have following equivalences atan2 y 1 x 1 atan2 y 2 x 2 atan2 y 1 x 1 atan2 y 2 x 2 by 1 Arg x 1 i y 1 Arg x 2 i y 2 by 2 Arg e i Arg x 1 i y 1 Arg x 2 i y 2 by 3 Arg e i Arg x 1 i y 1 e i Arg x 2 i y 2 Arg x 1 i y 1 x 2 i y 2 by 4 Arg x 1 x 2 y 1 y 2 i y 1 x 2 y 2 x 1 atan2 y 1 x 2 y 2 x 1 x 1 x 2 y 1 y 2 by 2 displaystyle begin aligned operatorname atan2 y 1 x 1 pm operatorname atan2 y 2 x 2 amp operatorname atan2 y 1 x 1 operatorname atan2 pm y 2 x 2 amp text by 1 amp operatorname Arg x 1 iy 1 operatorname Arg x 2 pm iy 2 amp text by 2 amp operatorname Arg e i operatorname Arg x 1 iy 1 operatorname Arg x 2 pm iy 2 amp text by 3 amp operatorname Arg e i operatorname Arg x 1 iy 1 e i operatorname Arg x 2 pm iy 2 amp operatorname Arg x 1 iy 1 x 2 pm iy 2 amp text by 4 amp operatorname Arg x 1 x 2 mp y 1 y 2 i y 1 x 2 pm y 2 x 1 amp operatorname atan2 y 1 x 2 pm y 2 x 1 x 1 x 2 mp y 1 y 2 amp text by 2 end aligned Corollary if y 1 x 1 displaystyle y 1 x 1 and y 2 x 2 displaystyle y 2 x 2 are 2 dimensional vectors the difference formula is frequently used in practice to compute the angle between those vectors with the help of atan2 displaystyle operatorname atan2 since the resulting computation behaves benign in the range p p displaystyle pi pi and can thus be used without range checks in many practical situations East counterclockwise north clockwise and south clockwise conventions etc EditThe a t a n 2 displaystyle mathrm 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 a t a n 2 displaystyle mathrm atan2 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 a t a n 2 y x displaystyle mathrm atan2 y x East Counterclockwise Convention a t a n 2 x y displaystyle mathrm atan2 x y North Clockwise Convention a t a n 2 x y displaystyle mathrm atan2 x y South Clockwise Convention As an example let x 0 3 2 displaystyle x 0 frac sqrt 3 2 and y 0 1 2 displaystyle y 0 frac 1 2 then the east counterclockwise format gives a t a n 2 y 0 x 0 180 p 30 displaystyle mathrm atan2 y 0 x 0 cdot frac 180 pi 30 circ the north clockwise format gives a t a n 2 x 0 y 0 180 p 60 displaystyle mathrm atan2 x 0 y 0 cdot frac 180 pi 60 circ and the south clockwise format gives a t a n 2 x 0 y 0 180 p 120 displaystyle mathrm atan2 x 0 y 0 cdot frac 180 pi 120 circ Apparently changing the sign of the x and or y arguments and swapping their positions can create 8 possible variations of the a t a n 2 displaystyle mathrm 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 EditThe realization of the function differs from one computer language to another In Microsoft Excel 6 OpenOffice org Calc LibreOffice Calc 7 Google Spreadsheets 8 iWork Numbers 9 and ANSI SQL 2008 standard 10 the 2 argument arctangent function has the two arguments in the standard sequence Re Im displaystyle operatorname Re operatorname Im reversed relative to the convention used in the discussion above In Mathematica the form ArcTan i x i i y i 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 P8 and has the arguments Re Im displaystyle operatorname Re operatorname Im On TI 85 the arg 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 x i y x y The Im Re displaystyle operatorname Im operatorname 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 p p 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 x coordinate atan i y i i x i 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 see the section Why don t you compile Matlab Python R code to Julia 13 For systems implementing signed zero infinities or Not a Number for example IEEE floating point it is common to implement reasonable extensions which may extend the range of values produced to include p and 0 when y 0 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 14 It can deal with infinities and results lie in the closed interval p p e g atan2 i x i p 2 for finite x Particularly FPATAN is defined when both arguments are zero atan2 0 0 0 atan2 0 0 p atan2 0 0 0 atan2 0 0 p This 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 15 and Tan 1 16 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 Atan y x Arg x i y On HP calculators treat the coordinates as a complex number and then take the ARG Or lt lt C gt R ARG gt gt ATAN2 STO On scientific calculators the function can often be calculated as the angle given when x y is converted from rectangular coordinates to polar coordinates Systems supporting symbolic mathematics normally return an undefined value for atan2 0 0 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 atan2 can be implemented in a numerically reliable manner by the CORDIC method Thus implementations of atan y will probably choose to compute atan2 y 1 See also EdithypotReferences Edit 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 doi 10 1016 j renene 2021 03 047 S2CID 233631040 Microsoft Excel Atan2 Method Microsoft LibreOffice Calc ATAN2 Libreoffice org Functions and formulas Docs Editors Help support google com Numbers Trigonometric Function List Apple ANSI SQL 2008 standard Teradata Archived from the original on 2015 08 20 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 amp 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 amp Business Media ISBN 9789048194438 Retrieved 20 April 2018 via Google Books External links EditATAN2 Online calculator Java 1 6 SE JavaDoc atan2 at Everything2 PicBasic Pro solution atan2 for a PIC18FOther implementations code for atan2 Bearing Between Two Points Archived from the original on 18 November 2020 Retrieved 21 February 2022 Arctan and Polar Coordinates Archived from the original on 18 October 2018 Retrieved 21 February 2022 What s Arccos Archived from the original on 6 September 2017 Retrieved 21 February 2022 Notes Edit Assuming the definitions undefined 0 0 displaystyle text undefined cdot 0 0 undefined 1 undefined displaystyle text undefined cdot 1 text undefined and z undefined undefined displaystyle z text undefined text undefined for any z displaystyle z One can apply the periodicity of the result to map to another desired range e g mapping to 0 2p by adding 2p to the negative results Retrieved from https en wikipedia org w index php title Atan2 amp oldid 1136376231, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.