PHP Function Reference

PHP atan2() Function



The PHP atan2() function returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). The returned value will be in the range -𝜋 through 𝜋. In special cases it returns the following:

  • If either argument is NAN, then the result is NAN.
  • If both arguments are positive infinity, then the result is closest to 𝜋/4.
  • If the first argument is positive or negative finite and the second argument is positive infinity, then the result is zero.
  • If the first argument is positive and finite and the second argument is negative infinity, then the result is closest to 𝜋.
  • If the first argument is negative and finite and the second argument is negative infinity, then the result is closest to -𝜋.
  • If the first argument is positive and the second argument is zero, or the first argument is positive infinity and the second argument is finite, then the result is closest to 𝜋/2.
  • If the first argument is negative and the second argument is zero, or the first argument is negative infinity and the second argument is finite, then the result is closest to -𝜋/2.
  • If the first argument is positive infinity and the second argument is negative infinity, then the result is closest to 3*𝜋/4.
  • If the first argument is negative infinity and the second argument is positive infinity, then the result is closest to -𝜋/4.
  • If both arguments are negative infinity, then the result is closest to -3*𝜋/4.

Syntax

atan2(y, x)

Parameters

y Required. Specify the ordinate coordinate.
x Required. Specify the abscissa coordinate.

Return Value

Returns theta of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

Example:

In the example below, atan2() function is used to calculate the theta of a point.

<?php
echo "atan2(10, 10) = ".atan2(10, 10)."\n";
echo "atan2(10, 20) = ".atan2(10, 20)."\n";
echo "atan2(-10, 20) = ".atan2(-10, 20)."\n";
echo "atan2(10, NAN) = ".atan2(10, NAN)."\n";
echo "atan2(10, INF) = ".atan2(10, INF)."\n";
echo "atan2(INF, 10) = ".atan2(INF, 10)."\n";
?>

The output of the above code will be:

atan2(10, 10) = 0.78539816339745
atan2(10, 20) = 0.46364760900081
atan2(-10, 20) = -0.46364760900081
atan2(10, NAN) = NAN
atan2(10, INF) = 0
atan2(INF, 10) = 1.5707963267949

❮ PHP Math Reference