+ 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 ?!?! 🤔🤔🤔🤔

11th Aug 2022, 8:34 AM
Saeed
Saeed - avatar
10 odpowiedzi
11th Aug 2022, 8:53 AM
PanicS
PanicS - avatar
+ 3
Thank you PanicS for your great answer, but isn't it a serious bug? It's not normal at all!
11th Aug 2022, 9:01 AM
Saeed
Saeed - avatar
+ 2
Manav Roy I checked java! Also in jave it prints -2 😐 but in python it shows 4 !
11th Aug 2022, 8:47 AM
Saeed
Saeed - avatar
+ 2
Oma Falk What does Guido mean? Guido van Rossum, the creator of python?
11th Aug 2022, 11:00 AM
Saeed
Saeed - avatar
+ 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.
12th Aug 2022, 2:03 PM
narcoan
+ 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.
11th Aug 2022, 9:24 AM
Oma Falk
Oma Falk - avatar
+ 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.
11th Aug 2022, 11:48 AM
PanicS
PanicS - avatar
+ 1
BTW... -2 and 4 are congruent with Respekt to mod 6
11th Aug 2022, 12:40 PM
Oma Falk
Oma Falk - avatar
0
I asked something along the same line sometime ago https://www.sololearn.com/discuss/3065059/?ref=app
13th Aug 2022, 1:30 AM
Ipang