Skip to content

Core\Math::remainder

Core\Math::remainder(float $a, float $b): floatCore\Math::remainder(
    float $a,
    float $b,
): float

Core\Math::remainder returns the remainder after dividing one float by another. For whole numbers, use the % operator.

The remainder has the same sign as the first value. So Core\Math::remainder(-7.5, 2.0) is -1.5, and the sign of the second value does not change the result. To get a result that is never negative, add the absolute value of the second value to the result. Then take the remainder of that sum again.

When the second value is an infinity, the result is the first value. When the first value is an infinity, the result is NaN (a value that means "not a number"). When either value is NaN, the result is also NaN.

When the second value is zero, Core\Math::remainder throws an ArithmeticError.

The examples below show remainders with both signs, then zero and infinity, then a compass heading that stays between 0 and 360 degrees.

Parameters

  • $afloatThe number to divide.
  • $bfloatThe number to divide by. It may not be zero.

Return value

Returns float. The remainder. remainder(7.5, 2.0) is 1.5, and remainder(-7.5, 2.0) is -1.5. When $a is an infinity, the result is NaN. When $b is an infinity, the result is $a.

Errors

  • ArithmeticErrorWhen $b is zero.

Changelog

VersionChange
0.0.6Core\Math::quotient divides two whole numbers, divide divides two floats, and remainder returns what is left after a division. The old names were intDiv, fdiv and mod.

Examples

Remainders and signs

<?nvs
// Shows the remainder of a division, and the sign it has.
// 2.0 fits into 7.5 three times, and 1.5 is left. This prints 1.5.
echo Core\Math::remainder(7.5, 2.0), "\n";
// The result has the sign of the first value. This prints -1.5, then 1.5.
echo Core\Math::remainder(-7.5, 2.0), "\n";
echo Core\Math::remainder(7.5, -2.0), "\n";
// 2.5 fits into 10.0 exactly, so nothing is left. This prints 0.
echo Core\Math::remainder(10.0, 2.5), "\n";
Output
1.5
-1.5
1.5
0

Zero and infinity

<?nvs
// Shows the result for an infinity, and what happens when you divide by zero.
// Any number fits into an infinity zero times. This prints 3.
echo Core\Math::remainder(3.0, Core\Math::INFINITY), "\n";
// An infinity has no remainder. This prints NAN.
echo Core\Math::remainder(Core\Math::INFINITY, 2.0), "\n";
// Dividing by zero throws an `ArithmeticError`.
try {
echo Core\Math::remainder(1.0, 0.0), "\n";
} catch (ArithmeticError $error) {
echo "You cannot divide by zero.\n";
}
Output
3
NAN
You cannot divide by zero.

Compass heading

<?nvs
// Shows how to keep a compass heading between 0 and 360 degrees after a turn.
class Compass
{
// Returns the heading as a value from 0 up to, but not including, 360.
public static function heading(float $degrees): float
{
// The first `remainder` can be negative. Adding 360 makes it positive, and the
// second `remainder` brings it back below 360.
return Core\Math::remainder(Core\Math::remainder($degrees, 360.0) + 360.0, 360.0);
}
}
// A ship at 350 degrees turns 20 degrees right. This prints 10.
echo Compass::heading(350.0 + 20.0), "\n";
// A ship at 0 degrees turns 30 degrees left. This prints 330.
echo Compass::heading(0.0 - 30.0), "\n";
// Two full circles and 5.5 degrees more. This prints 5.5.
echo Compass::heading(725.5), "\n";
Output
10
330
5.5