The modulus problem. (Can you explain it?) | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

The modulus problem. (Can you explain it?)

If you try to do the following operation: -1 % 3 In Python or Ruby, it will give you the result of 2. But if you try the exact same operation in Java, C, C++, Javascript, Kotlin, C# and PHP it will give you the previous -1 as result of the operation. why do this happen? how do Python and Ruby logically "think" to give this result of 2, and how do the other languages also "think" to return back this other result?

16th Mar 2018, 12:58 AM
Welliton Malta
Welliton Malta - avatar
1 Réponse
+ 4
From my answer to this Q&A, with explanation and demos: https://www.sololearn.com/Discuss/74000/?ref=app "Because in Python the remainder must be less than the divisor AND positive (Java and javascript choose the sign of the numerator instead)" In that Q&A I link a couple programs that show all +/- combinations for two languages (c++ and Python) but omitted the rest because you can learn easily enough which negative-values strategy (of two) your language has chosen.
16th Mar 2018, 1:58 AM
Kirk Schafer
Kirk Schafer - avatar