Fortran Wiki


atan2(y, x) computes the arctangent of the complex number X+iY-\\\\-\\\\\\\\X + i Y.

This function can be used to transform from Cartesian into polar coordinates and allows to determine the angle in the correct quadrant. To convert from Cartesian Coordinates (x,y)(x,y) to polar coordinates (r,\\theta):

\\begin{aligned} r &= \\sqrt{x^2 + y^2} \\\\ \\theta &= \\tan^{-1}(y / x) \\end{aligned}


FORTRAN 77 and later


Elemental function


result = atan2(y, x)


  • y - The type shall be real.
  • x - The type and kind type parameter shall be the same as y. If y is zero, then x must be nonzero.

Return value

The return value has the same type and kind type parameter as y. It is the principal value of the complex number X+iY(r,\\ \\X + i Y. If x is nonzero, then it lies in the range -\\pi \\leq \\atan (x) \\leq \\pi. The sign is positive if y is positive. If y is zero, then the return value is zero if x is strictly positive, \\pi if x is negative and y is positive zero (or the processor does not handle signed zeros), and -\\pi if x is negative and y is negative zero. Finally, if x is zero, then the magnitude of the result is \\pi/2.


program test_atan2
  real(4) :: x = 1.e0_4, y = 0.5e0_4
  x = atan2(y,x)
end program test_atan2

Note: In Return value, NNemec changed the first \\leq to \\lt. Joe Krahn reverted the edit, following the Fortran 2008 specs.

category: intrinsics