All Topics  
Atan2

 

   Email Print
   Bookmark   Link






 

Atan2



 
 
In trigonometry
Trigonometry

Trigonometry is a branch of mathematics that deals with triangle s, particularly those plane triangles in which one angle has 90 degrees . Trigonometry deals with relationships between the sides and the angles of triangles and with the trigonometric functions, which describe those relationships....
, the two-argument function
Function (mathematics)

The mathematical concept of a function expresses dependence between two quantities, one of which is known and the other which is produced. A function associates a single output to each input element drawn from a fixed Set , such as the real numbers , although different inputs may have the same output....
 atan2 is a variation of the arctangent function. For any real
Real number

In mathematics, the real numbers may be described informally in several different ways. The real numbers include both rational numbers, such as 42 and −23/129, and irrational numbers, such as pi and the square root of two; or, a real number can be given by an infinite decimal representation, such as 2.4871773339...., where the digits co...
 arguments x and y not both equal to zero, atan2(yx) is the angle in radian
Radian

The radian is a unit of plane angle, equal to 180/pi Degree , or about 57.2958 degrees, or about 57?17'45?. It is the standard unit of angular measurement in all areas of mathematics beyond the elementary level....
s between the positive x-axis of a plane and the point given by the coordinates
Cartesian coordinate system

In mathematics, the Cartesian coordinate system is used to determine each Point uniquely in a Plane through two numbers, usually called the x-coordinate or abscissa and the y-coordinate or ordinate of the point....
 (xy) on it. The angle is positive for counter-clockwise angles (upper half-plane, y > 0), and negative for clockwise angles (lower half-plane, y < 0).

The atan2 function was first introduced in computer programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s, but now it is also common in other fields of science and engineering.






Discussion
Ask a question about 'Atan2'
Start a new discussion about 'Atan2'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In trigonometry
Trigonometry

Trigonometry is a branch of mathematics that deals with triangle s, particularly those plane triangles in which one angle has 90 degrees . Trigonometry deals with relationships between the sides and the angles of triangles and with the trigonometric functions, which describe those relationships....
, the two-argument function
Function (mathematics)

The mathematical concept of a function expresses dependence between two quantities, one of which is known and the other which is produced. A function associates a single output to each input element drawn from a fixed Set , such as the real numbers , although different inputs may have the same output....
 atan2 is a variation of the arctangent function. For any real
Real number

In mathematics, the real numbers may be described informally in several different ways. The real numbers include both rational numbers, such as 42 and −23/129, and irrational numbers, such as pi and the square root of two; or, a real number can be given by an infinite decimal representation, such as 2.4871773339...., where the digits co...
 arguments x and y not both equal to zero, atan2(yx) is the angle in radian
Radian

The radian is a unit of plane angle, equal to 180/pi Degree , or about 57.2958 degrees, or about 57?17'45?. It is the standard unit of angular measurement in all areas of mathematics beyond the elementary level....
s between the positive x-axis of a plane and the point given by the coordinates
Cartesian coordinate system

In mathematics, the Cartesian coordinate system is used to determine each Point uniquely in a Plane through two numbers, usually called the x-coordinate or abscissa and the y-coordinate or ordinate of the point....
 (xy) on it. The angle is positive for counter-clockwise angles (upper half-plane, y > 0), and negative for clockwise angles (lower half-plane, y < 0).

The atan2 function was first introduced in computer programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s, but now it is also common in other fields of science and engineering. It dates back at least as far as the FORTRAN
Fortran

Fortran is a general-purpose programming language, procedural programming language, imperative programming language programming language that is especially suited to numerical analysis and scientific computing....
 programming language and is currently found in the C programming language's math.h
Math.h

math.h is a header file in the C standard library of C programming language designed for basic mathematical operations. Most of the functions involve the use of floating point numbers....
 standard library
C standard library

The C standard library consists of a set of sections of the ISO C standard which describe a collection of header files and library routines used to implement common operations, such as input/output and character string handling, in the C ....
, the Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
 Math library and elsewhere. Many scripting languages, such as Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
, include the C-style atan2 function.

In mathematical terms, atan2 computes the principal value
Principal value

In considering complex multiple-valued functions in complex analysis, the principal values of a function are the values along one chosen branch of that function, so it is Single-valued function....
 of the argument
Arg (mathematics)

In mathematics, arg is a function operating on complex numbers , and intuitively gives the angle between the line joining the point to the origin and the positive real number Cartesian coordinate system, shown as in figure 1 opposite, known as an argument of the point ....
 function applied to the complex number
Complex number

In mathematics, the complex numbers are an extension of the real numbers obtained by adjoining an imaginary unit, denoted i, which satisfies:...
 x+iy. That is atan2(yx) = Pr arg(x+iy) = Arg(x+iy). The argument can be changed by 2p corresponding to a complete turn round the origin without making any difference to the angle, the principal value is the value in the range (-p,p], that is -p < atan2(yx) = p.

The atan2 function is useful in many applications involving vectors in Euclidean space
Euclidean space

Around 300 Before Christ, the Ancient Greece mathematician Euclid undertook a study of relationships among distances and angles, first in a plane and then in space....
, such as finding the direction from one point to another. A principal use is in computer graphics rotations, for converting rotation matrix
Rotation matrix

In matrix theory, a rotation matrix is a real number square matrix whose transpose is its invertible matrix and whose determinant is 1 The matrix is so-called because it geometrically corresponds to a linear map that sends vectors to a corresponding vector rotated about the origin by a fixed angle....
 representations into Euler angles.

In some computer programming languages, the order of the parameters is reversed or a different name is used for the function. On scientific calculators the function can often be calculated as the angle given when (xy) is converted from rectangular coordinates to polar coordinates.

The associated function hypot, which is less widely defined, calculates the length of the hypotenuse of a right-angle triangle. This is easy to do using the square root function but hypot(xy) avoids possible problems with very large or very small numbers.

Motivation


The one-argument arctangent function does not distinguish between diametrically opposite directions. For example, the anticlockwise angle from the x-axis to the vector <1, 1>, calculated in the usual way as arctan(1/1), is p/4 (radians), or 45°. However, the angle between the x-axis and the vector <−1,−1> appears, by the same method, to be arctan(−1/−1), again p/4, even though the answer clearly should be −3p/4, or −135°.

The "atan2" function takes into account the signs of both vector components, and places the angle in the correct quadrant. Thus, atan2(1, 1) = p/4 and atan2(−1, −1) = −3p/4.

Additionally, the ordinary arctangent method breaks down when required to produce an angle of ±p/2 (or ±90°). For example, an attempt to find the angle between the x-axis and the vector <0,1> requires evaluation of arctan(1/0), which fails on division by zero. In contrast, atan2(1,0) gives the correct answer of p/2.

When calculations are performed manually, the necessary quadrant corrections and exception handling can be done by inspection, but in computer programs it is extremely useful to have a single function that always gives an unambiguous correct result.

Definition


For y?0:

where is the angle in [0,p/2) such that    and sgn is the sign function
Sign function

In mathematics, the sign function is an Even and odd functions function that extracts the negative and non-negative numbers of a real number....
.

And:

Notes:
  • This produces results in the range (-p,p], which can be mapped to [0,2p) by adding 2p to negative values.


  • Traditionally, atan2(0,0) is undefined.
    • The C function, and most other computer implementations, are designed to reduce the effort 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
      NaN

      In computing, NaN, which stands for Not a Number, is a value or symbol that is usually produced as the result of an operation on invalid input operands, especially in floating point calculations....
       (Not a Number).
    • Systems supporting symbolic mathematics should normally return an undefined value for atan2(0,0) or otherwise signal that an abnormal condition has arisen.


  • For systems, for example IEEE floating point, implementing signed zero, infinities
    Infinity

    Infinity comes from the Latin infinitas or "unboundedness." It refers to several distinct concepts – usually linked to the idea of "without end" – which arise in philosophy, mathematics, and theology....
    , or Not a Number it is usual to implement reasonable extensions which may extend the range of values produced to include -p and -0. These also may return NaN or raise an exception when given a NaN argument.


The free maths library FDLIBM (Freely Distributable LIBM) available from netlib
Netlib

Netlib is a repository of software for scientific computing maintained by AT&T, Bell Laboratories, the University of Tennessee and Oak Ridge National Laboratory....
 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
CORDIC

CORDIC is a simple and efficient algorithm to calculate hyperbolic function and trigonometric functions. It is commonly used when no hardware multiplier is available as the only operations it requires are addition, subtraction, bitshift and lookup table....
 method. Thus implementations of atan(y) will probably choose to compute atan2(y,1).

The following expression derived from the tangent half-angle formula
Tangent half-angle formula

In various applications of trigonometry, it is useful to rewrite the trigonometric functions in terms of rational functions of a new variable t. These identities are known collectively as the tangent half-angle formulae because of the definition of t....
 can also be used to define atan2. This expression may be more suited for symbolic use than the definition above. It is however unsuitable for computational use; the division overflows in a neighbourhood of the negative x axis and gives an NaN or raises an error for atan2(0,0).

Variations

  • In Common Lisp
    Common Lisp

    Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
    , where optional arguments exist, the atan function allows one to optionally supply the x coordinate as in (atan x y)
  • In Mathematica
    Mathematica

    Mathematica is a computational software program used widely in scientific, engineering, and mathematical fields and other areas of technical computing....
    , 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.
  • In Microsoft Excel
    Microsoft Excel

    Microsoft Excel is a spreadsheet-application written and distributed by Microsoft for Microsoft Windows and Mac OS X. It features calculation, graphing tools, pivot tables and a macro programming language called VBA ....
    , the atan2 function has the two arguments reversed. OpenOffice.org Calc
    OpenOffice.org Calc

    OpenOffice.org Calc is the spreadsheet component of the OpenOffice.org software package.Calc is similar to Microsoft Excel, with a roughly equivalent range of features....
     also reverses the arguments.
  • In the Intel Architecture assembler code, atan2 is known as FPATAN (floating-point partial arctangent) instruction. It can deal with infinities and results range from (closed interval), e.g. . Particularly, FPATAN is defined when both arguments are zero:**
    This definition is related with the concept of signed zero, i.e.
  • On the TI-89 calculator, the equivalent function is called R?P? and has the arguments reversed.


The hypot function


When converting to polar coordinates the magnitude of the hypotenuse from (0, 0) to (xy) can be calculated using: There is a problem with very large or small values of x and y in that their squares may go out of the range of the machine representation. The hypot function when available calculates the result without causing such problems. It is not as widely defined as the atan2 function as using the square root function is easy and normally fully satisfactory. Using both together gives:

r = hypot(x, y)
theta = atan2(y, x)


Illustrations


The left diagram below shows values of atan2 at selected points on the unit circle. The values, in radians, are shown in blue inside the circle. The four points (1,0), (0,1), (-1,0), and (0,-1) are labeled outside the circle. Note that the order of arguments is reversed; the function atan2(y,x) computes the angle corresponding to the point (x,y).

The right diagram below show values argument of complex number with principal value from 0 to 2pi in radians measured in anticlockwise (counterclockwise)
Clockwise

A clockwise motion is one that proceeds 'like the clock's hands': from the top to the right, then down and then to the left, and back to the top....
 direction.

The diagram below shows values of atan2 for points of unit circle. On X axis is complex angle
Complex plane

In mathematics, the complex plane is a geometric representation of the complex numbersestablished by the real axis and the orthogonal imaginary axis....
 of point.

It starts from 0 ( point (0,1) ) and goes in anticlockwise (counterclockwise)
Clockwise

A clockwise motion is one that proceeds 'like the clock's hands': from the top to the right, then down and then to the left, and back to the top....
 direction through points :
  • (0,1) with complex angle pi/2 in radians,
  • (-1,0) with complex angle pi,
  • (-1,-1) with complex angle 3pi/2,
to (1,0) with complex angle 0 = 2pi modulo
Modulo

The word modulo, in the mathematical community, is often used informally, in many imprecise ways. Generally, to say "A is the same as B modulo C" means, more-or-less, "A and B are the same except for differences accounted for or explained by C"....
 2pi

On this diagram one can see discontinuity of function atan2. When a point z is crossing the negative real axis, for example point z goes from (0,1) through (-1,0) to (0,-1). Its argument should go from pi/2 through pi to 3pi/2, but the output of atan2 ( principal value
Principal value

In considering complex multiple-valued functions in complex analysis, the principal values of a function are the values along one chosen branch of that function, so it is Single-valued function....
 of argument) goes from pi/2 to pi, jumps to -pi (discontinuity), and goes to -pi/2.



The diagrams below show 3D view of respectively atan2 and over a region of the plane.

Note that for atan2, rays emanating from the origin have constant values, but for atan lines passing through the origin have constant values.



See also

  • Arg (mathematics)
    Arg (mathematics)

    In mathematics, arg is a function operating on complex numbers , and intuitively gives the angle between the line joining the point to the origin and the positive real number Cartesian coordinate system, shown as in figure 1 opposite, known as an argument of the point ....
  • Complex number
    Complex number

    In mathematics, the complex numbers are an extension of the real numbers obtained by adjoining an imaginary unit, denoted i, which satisfies:...
  • Inverse trigonometric function
    Inverse trigonometric function

    In mathematics, the inverse trigonometric functions or cyclometric functions are the inverse functions of the trigonometric functions. The principal inverses are listed in the following table....
     including atan
  • Complex argument (continued fraction)


External links

  • (note argument order is reversed)
  • at Everything2


Other implementations/code for atan2