Negative number modulus

Consider the following expression:

(a - b) mod N

Which of the following is equivalent to the above expression?

1) ((a mod N) + (-b mod N)) mod N

2) ((a mod N) - (b mod N)) mod N

Also how (-b mod N) is computed, i.e. How is the mode of a negative number calculated?

Thanks.

+4
source share
3 answers

I don’t want to bother you with complex mathematical concepts, so I will try to make it simple. When we say that a = b (mod c), we simply say that ab is a multiple of c. This means that when we want to know what the value of mod c is, saying that it is a or ac or + c or + 1000 * c is true. So your 2 formulas are valid.

, , ? , , . Java, , mod b a , b. , a = 7, b = 3 N = 5, (a-b)% N = 4, -1.

, , , , . , 2 .

:

function mod (int a, int N)
  return (a%N+N)%N
+5
while(N < 0)
{
    N= N+MOD;
}

,
,

int mod(num ,modValue)
{
    return ( modValue- ( (-num) % modValue) );
}
0

All Articles