acos()
Today, we will discuss the acos() function in PHP. This function is used to get the arc cosine of a number, which is the angle whose cosine is the given number.
The acos() function in PHP returns the arc cosine (inverse cosine) of a number — that is, the angle whose cosine equals the given value. It is the mathematical reverse of cos(): where cos() turns an angle into a ratio, acos() turns that ratio back into an angle. The returned angle is expressed in radians.
This page covers the syntax, the valid input range, how to convert the result to degrees, edge cases like out-of-range input, and how acos() relates to the other PHP trigonometric functions.
Syntax
acos(float $num): float$num— a float in the range-1to1(inclusive). This is required because the cosine of any real angle always falls in that range.- Return value — the arc cosine of
$num, in radians, in the range0toπ(about0to3.14159). If$numis outside-1..1, the function returnsNAN(Not a Number).
A Basic Example
We pass 0.5 to acos() and it returns roughly 1.0472 radians — which is exactly π / 3, the angle whose cosine is 0.5.
Converting the Result to Degrees
Because acos() returns radians, you almost always convert to degrees for human-readable output. Use rad2deg() or multiply by 180 / pi():
<?php
$angle_in_radians = acos(0.5);
$angle_in_degrees = rad2deg($angle_in_radians);
echo $angle_in_degrees; // 60
?>So the angle whose cosine is 0.5 is 60 degrees. The reverse conversion — turning degrees into radians before calling a trig function — is done with deg2rad().
Common Values
These reference points are useful to memorize when working with acos():
<?php
echo acos(1); // 0 (cos 0° = 1)
echo "\n";
echo acos(0); // 1.5707963… (π/2, i.e. 90°)
echo "\n";
echo acos(-1); // 3.1415926… (π, i.e. 180°)
?>Handling Out-of-Range Input
If you pass a value smaller than -1 or larger than 1, the cosine of no real angle could produce it, so acos() returns NAN. Guard against this when the input comes from user data or calculations:
<?php
$value = 2; // outside the valid -1..1 range
if ($value >= -1 && $value <= 1) {
echo rad2deg(acos($value));
} else {
echo "Input must be between -1 and 1.";
}
// Output: Input must be between -1 and 1.
?>You can also detect the result with is_nan() after calling the function.
When Would I Use acos()?
acos() shows up whenever you need to recover an angle from a cosine ratio:
- Geometry and graphics — finding the angle between two vectors using the dot-product formula.
- Navigation — computing bearings and great-circle distances on a sphere.
- Physics — resolving angles in projectile motion or wave problems.
It belongs to the same family as asin() (arc sine) and atan() (arc tangent). For the hyperbolic variant, see acosh().
Conclusion
The acos() function returns the arc cosine of a number as an angle in radians, accepting input from -1 to 1 and returning NAN for anything outside that range. Remember to convert the result to degrees with rad2deg() when you need readable output, and to validate input that may fall outside the valid domain.