Remainder of numbers in python | Sololearn: Learn to code for FREE!

+3

Remainder of numbers in python

Can anyone answer me why?! -20%6 = 4 Why not -2 ? (6*-3)+(-2)=-20 But in here in python it equals 4 ?!?! 🤔🤔🤔🤔

8/11/2022 8:34:18 AM

Saeed

10 Answers

New Answer

+3

I never noticed this in python. I looked on internet and find this: https://stackoverflow.com/questions/3883004/the-modulo-operation-on-negative-numbers-in-python

+3

Thank you PanicS for your great answer, but isn't it a serious bug? It's not normal at all!

+2

Manav Roy I checked java! Also in jave it prints -2 😐 but in python it shows 4 !

+2

Oma Falk What does Guido mean? Guido van Rossum, the creator of python?

+2

In school they teached us that the modulo operation gives strictly a positive integer, so that's why it gives 4 and -2. I guess python just follows the rules of maths.

+1

ohhhh there are many bugs in programming eg: a = a+1 Implement modulo in the most useful manner. Guido and the fathers of C-languages and the fathers of Java had different opinions... that's all. Guidos idea was that the modulo 6 repeats every 6 numbers. 10%6 == 4%6 == -2%6 ==-8%6 == -14%6 == -20% 6 ==4. Seems logical to me.

+1

I just find answer on internet, I don't know much python i code mostly with javascript. It can make bugs in your code if you dont know that this is diferent than in other language, but I don't think this is bug in language itself, this can be useful in some specific situation.

+1

BTW... -2 and 4 are congruent with Respekt to mod 6

0

I asked something along the same line sometime ago https://www.sololearn.com/discuss/3065059/?ref=app