2 ms·
> Careful what you wish for. What should -7 / 2 return? -3 kind of makes sense, because 7/2 is 3. -4 also makes sense. > It also desirable to have the remaind
by masklinn 5y ago
> Careful what you wish for. What should -7 / 2 return? -3 kind of makes sense, because 7/2 is 3.
-4 also makes sense.
> It also desirable to have the remainder consistent with the result of division, such that x = d*r + mod, where r is the result of division. Combine that with the above requires negative mods.
Python's behaviour is consistent with that definition:
>>> divmod(-7, 2)
(-4, 1)
-7 = -4 * 2 + 1