Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 8 Online
OpenStudy (anonymous):

Hello! I just started how to program in python and I typed -8%5 and the result was 2 instead of -3. Could someone explain this to me

OpenStudy (anonymous):

your computer must be broke because that is wrong. how many times did you try it. are you using Idle? if so, post your interactive session using dpaste.com or pastebin.com http://dpaste.com/702500/

OpenStudy (anonymous):

i think he means '-'8%5, which I also return 2 as well. 8%5 without the negative sign returns 3. I don't know the exact reason, but my guess is how the '%' works for negative numbers, which i believe it takes the smaller of the closest negative multiple which in this case is '-10' rather than '-5'

OpenStudy (maitre_kaio):

-8 = (-2 * 5) + 2 Don't forget that the modulo is always a positive number.

OpenStudy (maitre_kaio):

It seems implementation differs from one language to another for negative numbers. http://en.wikipedia.org/wiki/Modulo_operation

OpenStudy (anonymous):

thanx guys

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!