Skip to content

Core\Math::quotient

Core\Math::quotient(int $a, int $b): intCore\Math::quotient(
    int $a,
    int $b,
): int

Divides one whole number by another and returns a whole number. The remainder is removed, so the result moves toward zero: 7 divided by 2 is 3, and -7 divided by 2 is -3. The % operator gives the remainder that this function removes.

Dividing by 0 throws an ArithmeticError. Dividing Core\Math::INT_MIN by -1 also throws an ArithmeticError, because the result is one more than Core\Math::INT_MAX.

Use it when only complete groups count: full pages, full boxes, full minutes. Core\Math::floor rounds a float down instead.

The examples below show a few divisions, then negative numbers and a division by zero. The last one turns a number of seconds into minutes and seconds.

Parameters

  • $aintThe integer to divide.
  • $bintThe integer to divide by. It may not be zero.

Return value

Returns int. The whole part of $a / $b. quotient(7, 2) is 3, and quotient(-7, 2) is -3. The % operator gives the remainder.

Errors

  • ArithmeticErrorWhen $b is zero. Also when $a is Core\Math::INT_MIN and $b is -1, because the result is one more than Core\Math::INT_MAX.

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

A few divisions

<?nvs
// Shows a few divisions whose result is a whole number.
// 7 divided by 2 is 3.5. The remainder is dropped, so this prints 3.
echo Core\Math::quotient(7, 2), "\n";
// 10 divided by 5 has no remainder. This prints 2.
echo Core\Math::quotient(10, 5), "\n";
// 9 is smaller than 10, so this prints 0.
echo Core\Math::quotient(9, 10), "\n";
Output
3
2
0

Negative numbers and zero

<?nvs
// Shows the result for negative numbers, and what happens when you divide by zero.
// The result moves toward zero. -3.5 becomes -3, so this prints -3.
echo Core\Math::quotient(-7, 2), "\n";
// Two negative numbers give a positive result. This prints 3.
echo Core\Math::quotient(-7, -2), "\n";
// Dividing by zero throws an `ArithmeticError`.
try {
echo Core\Math::quotient(7, 0), "\n";
} catch (ArithmeticError $error) {
echo "You cannot divide by zero.\n";
}
Output
-3
3
You cannot divide by zero.

Minutes and seconds

<?nvs
// Shows how to turn a number of seconds into minutes and seconds, as a video player does.
class Duration
{
// `Core\Math::quotient` counts the full minutes. The `%` operator gives the seconds left over.
public static function describe(int $seconds): void
{
int $minutes = Core\Math::quotient($seconds, 60);
int $rest = $seconds % 60;
echo $seconds, ' seconds is ', $minutes, ' minutes and ', $rest, " seconds\n";
}
}
// This prints 2 minutes and 5 seconds.
Duration::describe(125);
// This prints 60 minutes and 0 seconds.
Duration::describe(3600);
// Less than one full minute. This prints 0 minutes and 59 seconds.
Duration::describe(59);
Output
125 seconds is 2 minutes and 5 seconds
3600 seconds is 60 minutes and 0 seconds
59 seconds is 0 minutes and 59 seconds