From: Dave Thomas Date: 2000-06-06T02:00:13-05:00 Subject: [ruby-talk:03109] Is divmod dangerous? In (say) Python, the modulus operator (%) is actually a remainder function too; Python arranges the quotient of integer division so that the remainder equals the modulo. In Ruby, % is a true modulo operator, and a.remainder(b) is used to get the remainder. Ruby Python a b | a/b a%b a.remainder(b) | a/b a%b ============|=============================|================= 13 4 | 3 1 1 | 3 1 13 -4 | -3 -3 1 | -4 -3 -13 4 | -3 3 -1 | -4 3 -13 -4 | 3 -1 -1 | 3 -1 Why do we care, you ask? Well, both Ruby and Python (and Haskell and ...) have divmod, which returns a quotient and a . In (say) Python, if (q, r) = divmod(x, y) then x = q*y + r In Ruby, however, divmod returns the quotient and modulo: for x in [13, -13] for y in [4, -4] q, r = x.divmod(y) puts q*y + r end end => 13, 9, -9, -13 This strikes me as dangerous, as I suspect most people would expect divmod to return a remainder, not a modulus. Anyone have any opinions? Dave