More on integer division
Simon Marlow
simonmar@microsoft.com
Mon, 1 Jul 2002 10:49:00 +0100
> After a looking a little more, there seem to be other problems
> (including errors in my proposed solution). I don't know where the
> code for quotRem is, but it is also buggy. For instance,
>=20
> Prelude> 9 `quotRem` (-5)
> (-1,4)
>=20
> (The correct answer is (-1,-4).) I'm frankly astonished: has noone
> used these functions with negative arguments before?
GHC's answer looks right to me, since according to the report:
(x `quot` y)*y + (x `rem` y) =3D=3D x
so
(-1 * -5) + 4 =3D=3D 9
but if 9 `rem` (-5) should be -4, then
(-1 * -5) + -4 =3D 1
> I'm shocked that non of the three Haskell implementations had a test
> suite that caught this problem.
Take a look at
fptools/testsuite/tests/ghc-regress/numeric/should_run/arith0011.hs.
There may be one or two wrong answers lurking in there, but that's a lot
of results to check by hand!
Cheers,
Simon