3 ms·
Good point. Interestingly enough, Python seems to handle this appropriately: >>> -4%2 0 >>> -8%2 0 >>> -9%2 1
by craftyguy 9y ago
Good point. Interestingly enough, Python seems to handle this appropriately:
>>> -4%2
0
>>> -8%2
0
>>> -9%2
1
- panic 9y agoYeah, Python is one of the few languages which actually defines sane behavior for integer division and modulus rather than leaving it up to the hardware. I wish more languages would follow its example!
- triska 9y agoProlog is also like this: ?- x is -4 mod 2. X = 0. ?- X is -8 mod 2. X = 0. ?- X is -9 mod 2. X = 1. Using declarative integer arithmetic, we can even express very generally that a number X is a multiple of 2: ?- X #= 2*_. This is true iff there is any integer (indicated by an anonymous variable _) that, multiplied by 2, yields X. It works for positive and negative integers. For example: ?- -4 #= 2*_. true. ?- -9 #= 2*_. false. A very nice reference on the general topic of bitwise operations is Donald Knuth's Bitwise Tricks and Techniques in Volume 4 of The Art of Computer Programming.
- bakery2k 9y agoGuido has written a blog post explaining "Why Python's Integer Division Floors" [1]. Python also uses floor division, and the corresponding "sign-of-modulo matches sign-of-divisor", for floating-point numbers. The blog post claims that this can be problematic in certain cases (compared to C's "sign-of-modulo matches sign-of-dividend"), but does not give any concrete examples. [1] http://python-history.blogspot.com.au/2010/08/why-pythons-integer-division-floors.html http://python-history.blogspot.com.au/2010/08/why-pythons-in...
- kazinator 9y agoISO C doesn't leave it up to the hardware. Integer division is required to truncate toward zero; the remainder harmonizes with that. (If someone designs a new HLL which leaves some arithmetic to the hardware that C doesn't, that is indeed silly.) The sane language here Common Lisp: it does it in all the ways rather than picking one approach with some hand-waving justification about why it's the good choice. It has truncate, round, ceiling and floor functions that all return a quotient and remainder. There are also mod and rem functions that calculate just the remainder from a floor and truncate, respectively.