In trigonometry, the twoargument function atan2 is a variation of the arctangent function. For any real arguments x and y not both equal to zero, atan2(y, x) is the angle in radians between the positive xaxis of a plane and the point given by the coordinates (x, y) on it. The angle is positive for counterclockwise angles (upper halfplane, y > 0), and negative for clockwise angles (lower halfplane, y < 0).
The atan2 function was first introduced in computer programming languages, but now it is also common in other fields of science and engineering. It dates back at least as far as the FORTRAN programming language and is currently found in C's math.h standard library, the Java Math library, the C# static Math class, and elsewhere. Many scripting languages, such as Perl, include the Cstyle atan2 function.^{[1]}
In mathematical terms, atan2 computes the principal value of the argument function applied to the complex number x+iy. That is atan2(y, x) = Pr arg(x+i y) = Arg(x+iy). The argument can be changed by 2π (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) ≤ π.
The atan2 function is useful in many applications involving vectors in Euclidean space, such as finding the direction from one point to another. A principal use is in computer graphics rotations, for converting rotation matrix 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 (x, y) is converted from rectangular coordinates to polar coordinates.
Contents 
The oneargument arctangent function does not distinguish between diametrically opposite directions. For example, the anticlockwise angle from the xaxis to the vector <1, 1>, calculated in the usual way as arctan(1/1), is π/4 (radians), or 45°. However, the angle between the xaxis and the vector <−1, −1> appears, by the same method, to be arctan(−1/−1), again π/4, even though the answer clearly should be −3π/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) = π/4 and atan2(−1, −1) = −3π/4.
Additionally, the ordinary arctangent method breaks down when required to produce an angle of ±π/2 (or ±90°). For example, an attempt to find the angle between the xaxis 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 π/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.
In terms of the standard arctan function, that is with range of (−π/2, π/2), it can be expressed as follows:
Notes:
The free maths 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).
The following expression derived from the tangent halfangle 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 floating point computational use as it is undefined for y=0,x<0 and may overflow near these regions. The formula gives an NaN or raises an error for atan2(0,0), but this is not an issue since atan2(0,0) is not defined.
A variant of the last formula is sometimes used in high precision computation. This avoids overflow but is always undefined when y=0:
The 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 diagram below shows values of atan2 for points of unit circle. On X axis is complex angle of point. It starts from 0 ( point (0,1) ) and goes in anticlockwise (counterclockwise) direction through points :
to (1,0) with complex angle 0 = 2pi modulo 2pi On this diagram one can clearly see the 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 of argument) goes from pi/2 to pi, jumps to pi (discontinuity), and goes to pi/2.^{[6]}
For anyone wondering about using atan2 to transform x and y coordinates into bearings from grid North, you can use bearing = atan2(x,y). The arguments in the above example are reversed to suit the preferences of the person who drew the diagram. Ironically, its a somewhat circular argument. Looking at the above diagram you can imagine that if you were to rotate it 90 degrees to anticlockwise and then flip it left to right, it would make a lot more sense from a geographic point of view.
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.
