Many languages have a "mod" or "%" operator that gives the remainder after division, truncated to 0; e.g. C, C ++ and Java, and possibly C #, will say:
(-11)/5 = -2 (-11)%5 = -1 5*((-11)/5) + (-11)%5 = 5*(-2) + (-1) = -11.
Haskell quot and rem are intended to simulate this behavior. I can imagine that compatibility with the release of some C program may be desirable in some far-fetched situation.
Haskell div and mod , and then Python / and%, follow the agreement of mathematicians (at least number theorists), always truncating down division (not in the 0 direction - to negative infinity), so the remainder is always non-negative. So in Python
(-11)/5 = -3 (-11)%5 = 4 5*((-11)/5) + (-11)%5 = 5*(-3) + 4 = -11.
The Haskell div and mod follow this behavior.
ShreevatsaR Dec 04 '08 at 7:52 2008-12-04 07:52
source share