Consider the following equations:
10 % 3 = 1
10 % -3 = -2
10001 % 30 = 11
10001 % -30 = -19
C#'s System.Numerics.BigInteger Seems to produce positive results for negative divisors, as if something like Abs(divisor) has been used in the calculation; for example:
10 % -3
BigInteger left = 10;
BigInteger right = -3;
Console.WriteLine(left % right); // 1, but should be -2
10001 % -30
BigInteger left = 10001;
BigInteger right = -30;
Console.WriteLine(left % right); // 11, but should be -19
Why is this? Is this a bug?
Modulus/remainder operations can have different behaviors with negative operands in different programming languages.
.NET consistently follows the convention specified in the BigInteger documentation:
So you should see the same behavior with
int,longetc - but that may be different behavior to other platforms (e.g. Wolfram Alpha). It happens to be the same in .NET as in Java - from the JLS remainder operator docs: