The difference between these methods is documented in Javadoc. From mod(m)
:
This method differs from remainder
in that it always returns a non-negative BigInteger.
In addition, this method throws an ArithmeticException
if this argument is negative, which is not your case according to your change. Thus, to check the divisibility, there will be no difference between mod
and remainder
: when one of them is 0, the other will be 0. You could just use remainder
, since mod
can do another calculation that you do not need.
To see the difference in action, consider the following:
public static void main(String[] args) { BigInteger a = BigInteger.valueOf(-2); BigInteger b = BigInteger.valueOf(3); System.out.println(a.remainder(b));
This is actually the same difference for the primitive int
a
and b
and the calculation of a % b
(which behaves like remainder
) and Math.floorMod(a, b)
(which behaves like mod
).
source share