Skip to content

Core\BigInt::remainder

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

Returns what is left over when this number is divided by another one, as a new Core\BigInt.

The result has the sign of the number you called it on, which is what % does with plain int values. -7 with a divisor of 2 gives -1, and 7 with a divisor of -2 gives 1. The sign of the divisor is never used.

dividedBy gives the quotient. The quotient times the divisor, plus the remainder, is the number you started with.

A divisor of zero throws an ArithmeticError.

Good to know: to get a result that is never negative, add $divisor->abs() to the result. Then call remainder on that sum with the same divisor.

Parameters

  • $divisorCore\BigIntThe value to divide by.

Return value

Returns Core\BigInt. A BigInt remainder. The remainder of -7 divided by 2 is -1. The quotient from dividedBy times $divisor, plus this remainder, is the original value.

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

What a division leaves over

<?nvs
// Shows how to get what is left over from a division of large numbers.
var $total = Core\BigInt::parse('1000000000000000000000000');
// 1000000000000000000000000 divided by 7000000000000 leaves something over.
// `remainder` returns that part, so this prints 1000000000000.
echo $total->remainder(Core\BigInt::parse('7000000000000')), "\n";
// This division is exact, so nothing is left over. It prints 0.
echo $total->remainder(Core\BigInt::parse('1000000000000000')), "\n";
Output
1000000000000
0

The sign of the result

<?nvs
// Shows which sign a remainder has, and what a divisor of zero does.
var $seven = Core\BigInt::of(7);
var $two = Core\BigInt::of(2);
// The result has the sign of the number you call `remainder` on. These two lines
// print 1 and -1.
echo $seven->remainder($two), "\n";
echo $seven->negated()->remainder($two), "\n";
// The sign of the divisor is not used. 7 with a divisor of -2 also gives 1,
// so this prints 1.
echo $seven->remainder($two->negated()), "\n";
// A divisor of zero is not allowed. `remainder` throws an `ArithmeticError`, and the
// message names the method that threw it.
try {
$seven->remainder(Core\BigInt::of(0));
} catch (ArithmeticError $error) {
echo $error->message, "\n";
}
Output
1
-1
1
Core\BigInt::remainder(): the divisor is zero

Gigabytes and the bytes left over

<?nvs
// Shows a number of bytes that is too large for an `int`, as whole gigabytes
// and the bytes that are left over.
var $perGigabyte = Core\BigInt::parse('1073741824');
var $written = Core\BigInt::parse('98765432109876543210');
// `dividedBy` gives the whole gigabytes and `remainder` gives the rest. Put together, the
// two are the number you started with.
echo $written->dividedBy($perGigabyte), ' GB and ', $written->remainder($perGigabyte), " bytes\n";
Output
91982476515 GB and 623279850 bytes