+ 1
Negative module
-5 % 3 = (1 -2*3) % 3 = 1 What is 1 inside bracket i mean where it comes from ?
4 ответов
+ 4
Adding to Ipang 's answer
If read the article again, it is written
(a + b) % n
= [(a % n) + (b % n)] % n
that is why -5 % 3 has been written as (1-2*3) % 3 because
(1 - 2*3) % 3
= [(1 % 3) - {(2 * 3) % 3}]
= [(1 % 3) - 0]
= 1
+ 3
I think you need to show full version of the code, as it is, it doesn't make sense.
Where you see this?
+ 2
Ipang I m sorry I will remember next time.
By the way the question was from here - https://www.geeksforgeeks.org/how-to-perform-modulo-with-negative-values-in-JUMP_LINK__&&__python__&&__JUMP_LINK/
+ 2
Gautam Yadav
It's ok, maybe now I see a bit clearer
I think maybe it is just an illustration (I could be wrong though),
1 - 2 * 3 equals to 1 - 6 which is -5
Thus (-5 % 3) and ((1 - 2 * 3) % 3) will give same result -> 1.
And here are some search result links to discussions about modulo operation with negative operand
https://www.sololearn.com/Discuss/2565566/?ref=app
https://www.sololearn.com/Discuss/2203461/?ref=app