Behaviour of div & mod with negative arguments?
Simon Marlow
simonmar@microsoft.com
Wed, 25 Sep 2002 10:03:22 +0100
> Does Haskell specify how div and mod should behave when
> given one or both arguments negative?
>=20
> Eg, in hugs we get:
>=20
> div 1 3 =3D 0
> div (-1) 3 =3D -1
> div 1 (-3) =3D -1
> div (-1) (-3) =3D 0
>=20
> 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 =3D (d,m) =20
such that d*b + m =3D=3D a=20
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 =3D (-1, 2) or (0, -1)
divMod gives you (-1,2), whereas quotRem gives you (0,-1).
Hope this helps.
Cheers,
SImon