Mathematics7 min readExamples run on PHP 8.5.10

atan()

Learn how PHP 8.4 atan() converts a tangent value into an angle in radians, with examples, special values, limits and common mistakes.

Signature

atan(float $num): float

Purpose

atan() returns the arc tangent of a number. It is the inverse of tan(): you give it a tangent value (a ratio of rise to run), and it gives you the angle that has that tangent. The angle is measured in radians, not degrees.

Typical uses include turning a slope into an angle, computing the direction of a line, and working with geometry, graphics or physics formulas.

Signature

Parameters and defaults

$num is the only parameter, and it has no default, so you must always pass it. It is the tangent value you want to invert. Any finite real number is in the domain of atan(), so unlike asin() or acos() there is no restricted input range such as -1 to 1.

Because the parameter is typed as float, integers are converted to floats automatically. Numeric strings such as "1.5" are accepted in the default weak typing mode. A non-numeric string, or an array, causes a TypeError.

Return value and failure cases

The function always returns a float: the angle in radians. For finite input the result lies strictly between -M_PI/2 and M_PI/2. The result is negative for negative input and positive for positive input.

atan() does not return false and does not emit warnings for numeric input. The only failure case you will meet in practice is a TypeError for an argument that cannot be treated as a float.

Example 1: Basic values

The tangent of 45 degrees is 1, so atan(1) is pi divided by four, about 0.7854. The example rounds the results so the output is stable.

Example
<?php
$values = [1, -1, sqrt(3)];
$result = [];
foreach ($values as $v) {
    $result[] = round(atan($v), 4);
}
echo json_encode($result), "\n";
Output
[0.7854,-0.7854,1.0472]

The third value, the square root of three, is the tangent of 60 degrees, which is pi/3 or about 1.0472 radians.

Example 2: Slope to angle in degrees

Most people think in degrees, so you normally combine atan() with rad2deg(). Here a helper computes the angle of a ramp from its rise and run and formats it with two decimals.

Example
<?php
function slopeAngle(float $rise, float $run): string
{
    return sprintf('%.2f', rad2deg(atan($rise / $run)));
}

echo slopeAngle(1, 2), "\n";
echo slopeAngle(1, 1), "\n";
echo slopeAngle(2, 1), "\n";
Output
26.57
45.00
63.43

Note that the helper divides before calling atan(). If $run is zero, the division throws DivisionByZeroError before atan() is even reached. See the edge cases below for the better tool in that situation.

Example 3: Infinity and NAN

atan() handles the special float values in a well-defined way. As the input grows without bound, the result approaches but never exceeds half of pi. Infinity gives exactly that limit, and NAN stays NAN.

Example
<?php
$checks = [
    atan(INF) === M_PI / 2,
    atan(-INF) === -M_PI / 2,
    is_nan(atan(NAN)),
];
echo json_encode($checks), "\n";
Output
[true,true,true]

Use is_nan() to test for NAN, because NAN is never equal to anything, including itself.

Edge cases

  • Zero: atan(0) is 0.0, as a float.
  • Very large values: results flatten out near plus or minus pi/2, so large inputs lose resolution. For example, 1e12 and 1e15 give nearly identical angles.
  • Floating point precision: the round trip tan(atan($x)) may differ from $x in the last digits. Compare with a tolerance or round before comparing.
  • Quadrants: atan() only returns angles in the right half of the circle (-90 to 90 degrees). It cannot tell the difference between the ratios 1/1 and -1/-1, because both reduce to the same value.

Common mistakes

  1. Expecting degrees. The result is in radians. Wrap it in rad2deg() when you need degrees.
  2. Using atan() for full-circle directions. If you have separate x and y components and need the correct quadrant, use atan2($y, $x) instead. It also handles a zero x value without a division.
  3. Dividing by zero first. Computing $y / $x yourself fails when $x is zero. Prefer atan2() for vector input.
  4. Comparing floats with ==. Rounded or tolerance-based comparison is safer than exact equality for computed angles.
  5. Confusing it with atanh(). atanh() is the inverse hyperbolic tangent and has a different domain (-1 to 1, exclusive).
  6. Passing non-numeric strings. These raise a TypeError; validate user input before calling the function.
  • tan(): the tangent, which atan() inverts.
  • atan2(): the two-argument arc tangent that returns the correct quadrant.
  • atanh(): inverse hyperbolic tangent.
  • asin() and acos(): arc sine and arc cosine.
  • rad2deg() and deg2rad(): convert between radians and degrees.

Recap

atan(float $num): float returns the angle in radians whose tangent is $num. It accepts any finite number, returns a value between -pi/2 and pi/2, maps infinity to the limits and NAN to NAN. Convert to degrees with rad2deg(), and reach for atan2() when you have x and y components or need a full-circle direction.

Tested examples

Basic arc tangent values rounded to four decimals
<?php
$values = [1, -1, sqrt(3)];
$result = [];
foreach ($values as $v) {
    $result[] = round(atan($v), 4);
}
echo json_encode($result), "\n";
Output
[0.7854,-0.7854,1.0472]
Converting a slope to an angle in degrees
<?php
function slopeAngle(float $rise, float $run): string
{
    return sprintf('%.2f', rad2deg(atan($rise / $run)));
}

echo slopeAngle(1, 2), "\n";
echo slopeAngle(1, 1), "\n";
echo slopeAngle(2, 1), "\n";
Output
26.57
45.00
63.43
Infinity and NAN behaviour
<?php
$checks = [
    atan(INF) === M_PI / 2,
    atan(-INF) === -M_PI / 2,
    is_nan(atan(NAN)),
];
echo json_encode($checks), "\n";
Output
[true,true,true]