By default, BigInteger::dividedBy() returns the exact result or throws a RoundingNecessaryException if there is a remainder.
To handle remainders, you can:
- Provide a
RoundingMode to dividedBy(). - Use
quotient() to get the integer part. - Use
remainder() to get the remainder. - Use
quotientAndRemainder() to get both at once.
// Default (throws exception if remainder exists)
echo BigInteger::of(999)->dividedBy(3); // 333
// echo BigInteger::of(1000)->dividedBy(3); // Throws RoundingNecessaryException
// Using RoundingMode
echo BigInteger::of(1000)->dividedBy(3, RoundingMode::Down); // 333
echo BigInteger::of(1000)->dividedBy(3, RoundingMode::Up); // 334
// Quotient and Remainder
echo BigInteger::of(1000)->quotient(3); // 333
echo BigInteger::of(1000)->remainder(3); // 1
[$quotient, $remainder] = BigInteger::of(1000)->quotientAndRemainder(3);