Skip to content

Core\Math::acos

Returns the angle whose cosine is the number you give it. This is the arc cosine.

The number must be from -1.0 to 1.0, because a cosine is always in that range. The angle is in radians, from 0.0 to PI. Core\Math::toDegrees converts it to degrees. A number outside the range gives NaN (a value that means "not a number"), and Core\Math::isNan tests for it.

Good to know: a cosine you calculated can end up a tiny bit past 1.0 because of rounding, and then the result is NaN. Use Core\Math::clamp to keep the number in range first.

The examples below show the angles for a few cosines, then what happens outside the range, then the angle between two directions.

Parameters

  • $nfloatA cosine: a number from -1.0 to 1.0.

Return value

Returns float. An angle from 0.0 to PI, in radians. A $n outside -1.0 to 1.0 gives NaN.

Examples

The angle for a cosine

<?nvs
// Shows the angles that `Core\Math::acos` returns for a few cosines.
// A cosine of 1.0 belongs to an angle of 0. This prints 0.
echo Core\Math::acos(1.0), "\n";
// The angle is in radians. A cosine of 0.0 gives half of PI, so this prints 1.5707963267949.
echo Core\Math::acos(0.0), "\n";
// `Core\Math::toDegrees` converts radians to degrees. This prints 90.
echo Core\Math::toDegrees(Core\Math::acos(0.0)), "\n";
// A cosine of -1.0 gives the largest angle, PI. This prints 180.
echo Core\Math::toDegrees(Core\Math::acos(-1.0)), "\n";
Output
0
1.5707963267949
90
180

A cosine outside the range

<?nvs
// Shows what `Core\Math::acos` returns for a number that is not a cosine,
// and how to keep a calculated cosine in range.
// No angle has a cosine of 1.5, so the result is `NaN`. This prints NAN.
echo Core\Math::acos(1.5), "\n";
// `Core\Math::isNan` tests for that result. This prints "not an angle".
echo Core\Math::isNan(Core\Math::acos(1.5)) ? 'not an angle' : 'an angle', "\n";
// A calculation can end a tiny bit past 1.0 because of rounding.
float $cosine = 1.0000000000000002;
// `Core\Math::clamp` moves it back to 1.0 first. This prints 0.
echo Core\Math::acos(Core\Math::clamp($cosine, -1.0, 1.0)), "\n";
Output
NAN
not an angle
0

The angle between two directions

<?nvs
// Shows how a delivery app finds the angle between the direction a driver is
// going and the direction of the next stop.
class Heading
{
// Returns the angle between two directions, in whole degrees.
public static function turn(float $ax, float $ay, float $bx, float $by): float
{
// The cosine of the angle is the dot product divided by both lengths.
float $cosine = ($ax * $bx + $ay * $by)
/ (Core\Math::hypot($ax, $ay) * Core\Math::hypot($bx, $by));
// Rounding can move the cosine a tiny bit past 1.0, so it is kept in range.
float $angle = Core\Math::acos(Core\Math::clamp($cosine, -1.0, 1.0));
return Core\Math::round(Core\Math::toDegrees($angle));
}
}
// Driving east, the next stop is to the north-east. This prints 45.
echo Heading::turn(1.0, 0.0, 3.0, 3.0), "\n";
// Driving east, the next stop is to the north. This prints 90.
echo Heading::turn(1.0, 0.0, 0.0, 2.0), "\n";
// Driving east, the next stop is behind. This prints 180.
echo Heading::turn(1.0, 0.0, -5.0, 0.0), "\n";
Output
45
90
180