Skip to content

Core\BigInt::dividedBy

$bigInt->dividedBy(Core\BigInt $divisor): Core\BigInt$bigInt->dividedBy(
    Core\BigInt $divisor,
): Core\BigInt

Returns the whole part of this number divided by another one, as a new Core\BigInt.

The fraction is dropped. A positive and a negative result are both cut toward zero, so 7 divided by 2 is 3, and -7 divided by 2 is -3. That is the same direction Core\Math::quotient cuts a plain int division.

Dividing by zero throws an ArithmeticError. Check the divisor with sign first when it may be zero.

dividedBy does not change either number. It returns the result as a new number. To get what the division left over, use remainder on the same two numbers.

Parameters

  • $divisorCore\BigIntThe value to divide by.

Return value

Returns Core\BigInt. A BigInt quotient. -7 divided by 2 is -3, and 7 divided by -2 is also -3.

Errors

  • ArithmeticError$divisor is zero.

Changelog

VersionChange
0.0.6Each Core\BigInt method that computes a new number is named for its result: plus, minus, multipliedBy, dividedBy, remainder, raisedTo, raisedToModulo, negated, shiftedLeft and shiftedRight. The old names were add, sub, mul, div, mod, pow, powMod, neg, shl and shr.

Examples

Dividing two numbers

<?nvs
// Shows how to divide one large number by another. `Core\BigInt::dividedBy` returns
// the whole part of the division.
var $total = Core\BigInt::parse('1000000000000000000000000');
// 1000000000000000000000000 divided by 7000000000000 leaves a remainder. `dividedBy`
// returns the whole part, so this prints 142857142857.
echo $total->dividedBy(Core\BigInt::parse('7000000000000')), "\n";
// This division is exact and has nothing left over. It prints 1000000000.
echo $total->dividedBy(Core\BigInt::parse('1000000000000000')), "\n";
Output
142857142857
1000000000

The direction it rounds

<?nvs
// Shows which way `dividedBy` cuts a division that is not exact, and what a divisor
// of zero does.
var $seven = Core\BigInt::of(7);
var $two = Core\BigInt::of(2);
// 7 divided by 2 is 3.5. The fraction is dropped, so this prints 3.
echo $seven->dividedBy($two), "\n";
// -7 divided by 2 is -3.5. The cut is toward zero, so this prints -3.
echo $seven->negated()->dividedBy($two), "\n";
// A divisor of zero is not allowed. `dividedBy` throws an `ArithmeticError`, and the
// message names the method that threw it.
try {
$seven->dividedBy(Core\BigInt::of(0));
} catch (ArithmeticError $error) {
echo $error->message, "\n";
}
Output
3
-3
Core\BigInt::dividedBy(): the divisor is zero

Whole units of a balance

<?nvs
// Shows how to report a balance that is stored in its smallest unit. One coin
// is 1000000000000000000 of those units, which is more than an `int` can store.
var $perCoin = Core\BigInt::parse('1000000000000000000');
array<string> $balances = [
'25000000000000000000',
'999999999999999999',
'7500000000000000000',
];
foreach ($balances as string $raw) {
// `dividedBy` gives the number of whole coins. Less than one coin is left out, so
// the three lines print 25, 0 and 7.
echo Core\BigInt::parse($raw)->dividedBy($perCoin), " whole coins\n";
}
Output
25 whole coins
0 whole coins
7 whole coins