PHP acos() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

The PHP acos() function returns the arc cosine of a number, which is the angle whose cosine is that number.

The angle is given in radians, and the input must be between -1 and 1, otherwise the result is NAN.

Syntax

Call acos() with the values you need. Start with this line.

PHP
$angle = acos($num);
CallWhat it does
acos(1)Returns 0 because the cosine of 0 radians is 1.
acos(0)Returns about 1.5708 (pi / 2).
acos(-1)Returns about 3.1416 (pi).

acos() is the opposite of cos(). Use rad2deg() if you want the answer in degrees.

Library declarations
PHP
acos(float $num): float

Parameters

ParameterDescription
numRequired. The value to work on. It must be in the range -1 to 1 to get a real result.

Return Value

Return ValueDescription
floatThe arc cosine of $num in radians, from 0 to pi. Returns NAN if $num is outside -1 to 1.

PHP Examples

Example 1 — Basic acos() usage

This example finds the arc cosine of a few common values and rounds them so the output is easy to read.

PHP
<?php
echo round(acos(0.5), 4) . PHP_EOL;
echo round(acos(0), 4) . PHP_EOL;
echo round(acos(-1), 4) . PHP_EOL;
echo acos(1) . PHP_EOL;

How It Works

  1. acos(0.5) is about 1.04719755 radians, and round(..., 4) shortens it to 1.0472.
  2. acos(0) is pi / 2, which rounds to 1.5708.
  3. acos(-1) is pi, which rounds to 3.1416.
  4. acos(1) is exactly 0, so PHP prints 0.

Example 2 — Values outside -1 to 1

The cosine of any real angle is between -1 and 1, so acos() has no answer for numbers outside that range. PHP returns NAN in that case.

PHP
<?php
var_dump(acos(2));

$value = -1.5;
$angle = acos($value);

if (is_nan($angle)) {
    echo "No real angle for $value" . PHP_EOL;
} else {
    echo "Angle: $angle" . PHP_EOL;
}

How It Works

  1. acos(2) is invalid because no angle has a cosine of 2.
  2. PHP does not throw an error here, it returns float(NAN).
  3. We store acos(-1.5) in $angle and test it with is_nan().
  4. Since the result is NAN, the first branch of the if statement runs.

Example 3 — Convert the angle to degrees

Radians are not always easy to read. Combine acos() with rad2deg() to get a result in degrees.

PHP
<?php
$radians = acos(0.5);
$degrees = rad2deg($radians);

echo "Radians: " . round($radians, 4) . PHP_EOL;
echo "Degrees: " . round($degrees, 2) . PHP_EOL;

How It Works

  1. acos(0.5) gives the angle whose cosine is 0.5, in radians.
  2. rad2deg() converts that angle from radians to degrees.
  3. The raw degree value is 60.000000000000014, so we round it to 2 decimals.
  4. The final output is 60 degrees, because cos(60 degrees) = 0.5.

Frequently Asked Questions

It returns the arc cosine of a number as a float in radians. The result is between 0 and pi.
PHP returns NAN because the cosine of a real angle can never be 2. No error or exception is thrown.
Radians. To convert to degrees, wrap the result in rad2deg().
cos() takes an angle and returns its cosine. acos() does the reverse: it takes a cosine value and returns the angle.
Use is_nan() on the result. If it returns true, the input was outside the range -1 to 1.

Next: acosh()

acosh() function returns the inverse hyperbolic cosine of a number.

acosh() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful