25 Sep
2002
25 Sep
'02
9:03 a.m.
Does Haskell specify how div and mod should behave when given one or both arguments negative?
Eg, in hugs we get:
div 1 3 = 0 div (-1) 3 = -1 div 1 (-3) = -1 div (-1) (-3) = 0
and so on.
We usually describe div as the version of division that "truncates towards negative infinity". What this actually means is that when there are two solutions to a `divMod` b = (d,m) such that d*b + m == a and abs m < b div picks the one where d is the closest to minus infinity, and quot picks the one where d is closer to zero. eg. (-1) `divMod` 3 = (-1, 2) or (0, -1) divMod gives you (-1,2), whereas quotRem gives you (0,-1). Hope this helps. Cheers, SImon