4 ms·
The key to this is in how you do integer division. Here is what happens in Python2: >>> 19 % -12 -5 >>> 19 / -12 -2 That is because Python2 integer divisio
by upofadown 8y ago
The key to this is in how you do integer division. Here is what happens in Python2:
>>> 19 % -12
-5
>>> 19 / -12
-2
That is because Python2 integer division is something called "floored division". This can be seen as always rounding toward negative infinity. This is arguably the correct way to do integer division and in fact there has been a lot of argument about this over the years.
One very nice feature of floored division is that it does not have a weird discontinuity around 0 as "round toward 0" does. This discontinuity can sometimes cause problems in embedded applications involving physical motion.
So mod and remainder are the same. You just have to do the division right...
- 1wd 8y agoGuido van Rossum has written about the background of this in Python here: http://python-history.blogspot.com/2010/08/why-pythons-integer-division-floors.html http://python-history.blogspot.com/2010/08/why-pythons-integ...
- daurnimator 8y agoLua also corrects things. See https://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/comments/ https://hisham.hm/2015/04/18/a-small-practical-example-where...
- DarkWiiPlayer 8y agoYay lua!