5%3 == 2
The remainder()
method works
the same as the remainder operator %
of int
.
Recall the rule for %
For example:
17 % 3 == 2 -17 % 3 == -2 17 % -3 == 2 -17 % -3 == -2
This works for BigInteger
s as well.
BigInteger n17 = new BigInteger( "-17" ); BigInteger p3 = new BigInteger( "3" ); System.out.println( n17.remainder( p3 ) ); // prints -2
An ArithmeticException
is thrown if the argument of remainder()
is zero.
For both int
and BigInteger
the original integer can be put back together:
quotient * divisor + remainder == theInteger
For example,
-17 / 3 == -5 // quotient -17 % 3 == -2 // remainder (-5) * 3 + (-2) == -15 + (-2) == -17
Another,
17 / -3 == -5 // quotient 17 % -3 == 2 // remainder (-5) * (-3) + 2 == 15 + 2 == 17
More Practice! Yay!!
Does remainder()
sometimes return a zero?