Mod of Negative Number Is Melting My Brain

C# modulo operator

There is no modulo operator in c#.

The % operator, is the remainder operator:https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/arithmetic-operators#remainder-operator-

The remainder operator % computes the remainder after dividing its left-hand operand by its right-hand operand.

Modulo of negative numbers

This solution is branchless, but performs % twice:

function wrapIndex(i, i_max) {
return ((i % i_max) + i_max) % i_max;
}

It should be said the C#/Java behavior of % is assumed, i.e. the result has the same sign as the dividend. Some languages define the remainder calculation to take the sign of the divisor instead (e.g. mod in Clojure). Some languages have both variants (mod/rem pair in Common Lisp, Haskell, etc). Algol-68 has %x which always returns a non-negative number. C++ left it up to implementation until C++11, now the sign of the remainder is (almost) fully specified according to the dividend sign.

See also

  • Wikipedia/Modulo operation

Modulo of a negative number

I don't want to bother you with some complex mathematical concepts, so i'll try to keep it simple.
When we say that a = b (mod c), we simply say that a-b is a multiple of c.
This means that when we want to know what is the value of a mod c, saying that it is a or a-c or a+c or a+1000*c is true.
Thus, your 2 formulas are valid.

But what you want is to know the answer that a computer would give to you, right ?
Well, it depends of the language you are using.
With Java for example, a mod b has the sign of a and has is absolute value strictly inferior to b. This means that with a = 7, b = 3 and N = 5, (a-b)%N = 4, but your two expressions will return -1.

What I would suggest you to do if you want to do arithmetics with modulos is to create your own mod function, so it always give you a positive integer for example. This way, your 2 expressions will always be equal to the original one.

An example here in pseudocode :

function mod (int a, int N)
return (a%N+N)%N

Modulo and Negative Number in Math?

In mathematics, the modulus must be >1 and the convention is that the modulo operation yields the smallest non-negative integer solution to a+km=b:

-7 mod  2 = 1
7 mod -2 Illegal
-7 mod -2 Illegal

It is easy to extend the definition to include negative numbers (and 1 and -1):

-7 mod  2 = 1
7 mod -2 = 1
-7 mod -2 = 1
-7 mod 3 = 2

But in computing, different languages use different conventions:

-7 mod  2 = {1, or -1, or undefined}

I suspect that your system says -7 mod 2 = -1, so that isEvenOrOdd(-7); yields 'even' (because -1 ≠ 1).

Weird Objective-C Mod Behavior for Negative Numbers

result = n % 3;
if( result < 0 ) result += 3;

Don't perform extra mod operations as suggested in the other answers. They are very expensive and unnecessary.

Modulus with negative numbers in C++

The thing is that the % operator isn't the "modulo operator" but the "division remainder" operator with the following equality

(a/b)*b + a%b == a    (for b!=0)

So, if in case your integer division rounds towards zero (which is mandated since C99 and C++11, I think), -5/4 will be -1 and we have

(-5/4)*4 + -5%4 == -5
-1 *4 -1 == -5

In order to get a positive result (for the modulo operation) you need to add the divisor in case the remainder was negative or do something like this:

long mod(long a, long b)
{ return (a%b+b)%b; }

Why is Modulo in MySQL (with negative number) giving unexpected results?

You could implement your own version of MOD using division as in this question but probably the simplest solution is to perform MOD twice, adding 50 between operations:

SELECT MOD(MOD(input, 50)+50, 50)

This will give you correct results regardless of the input value.



Related Topics



Leave a reply



Submit