atan

Returns the arc tangent of a number in radians, the inverse of tan().

Signature
atan(float $num): float
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$numfloatThe argument to process. Any real number is valid.

Return value

floatThe arc tangent of num in radians, in the range -PI/2 to PI/2.

Errors

Throws a TypeError if num cannot be coerced to float.

atan() turns a slope into an angle. Given a ratio of rise over run it returns the corresponding angle in radians, always between -M_PI/2 and M_PI/2. Unlike asin() and acos() it accepts every real number, including INF, which maps to exactly M_PI/2.

When to reach for it

Use it when you already hold a single ratio and only need its steepness: converting a road gradient or roof pitch to degrees, turning a regression coefficient into a trend angle, or finding the field of view implied by a sensor size and focal length. It is also a handy soft-clipping curve — atan() squashes any input into a bounded range while staying smooth and monotonic, which is why it appears in easing functions and in saturation models.

What trips people up

The single-argument form throws away information. atan($y / $x) cannot tell (-1, -1) from (1, 1), because the division collapses both to 1; you get 45 degrees when the real answer was -135 degrees. Any time you have separate x and y components, call atan2($y, $x) instead — it inspects both signs and covers the full circle.

A division also risks a division-by-zero error for a vertical line, whereas atan2() handles $x === 0 cleanly.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Convert a road gradient such as 0.08 into a slope in degrees.
  • Turn a linear regression coefficient into a trend angle for a chart annotation.
  • Compute a camera's field of view from sensor width and focal length.

Frequently asked questions

What is the difference between atan() and atan2()?

atan() takes one ratio and returns an angle limited to -90 to 90 degrees. atan2() takes the y and x components separately, uses their signs to pick the right quadrant, and returns an angle anywhere from -180 to 180 degrees.

Does atan() return degrees?

No, it returns radians. Pass the result through rad2deg() to get degrees.

Can atan() return NAN?

Not for a real input. atan() is defined for every real number, and atan(INF) returns PI/2. Only a NAN input produces NAN.

More examples

Roof pitch as an angle

A 4:12 pitch is 18.43 degrees, 6:12 is 26.57 degrees and 12:12 is exactly 45 degrees. atan() is what converts the builder's rise-over-run notation into the angle a protractor would show.

Example · php
<?php
$pitches = ['4:12' => 4 / 12, '6:12' => 6 / 12, '12:12' => 12 / 12];
foreach ($pitches as $label => $slope) {
    printf("%-6s %.2f degrees\n", $label, rad2deg(atan($slope)));
}

Where atan() loses the quadrant

The first line prints 45 because -1 / -1 is 1, exactly what a point in the first quadrant would give. The second prints -135, the true direction of the point, because atan2() sees both negative signs.

Example · php
<?php
$x = -1.0;
$y = -1.0;
echo round(rad2deg(atan($y / $x)), 1), PHP_EOL;
echo round(rad2deg(atan2($y, $x)), 1), PHP_EOL;

Discussion

  • Be the first to comment on this lesson.