[nhc-bugs] More on integer division
Dylan Thurston
dpt@math.harvard.edu
Sat, 29 Jun 2002 12:23:27 -0400
--DocE+STaALJfprDB
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
On Fri, Jun 28, 2002 at 03:54:50PM -0400, Dylan Thurston wrote:
> On Fri, Jun 28, 2002 at 10:24:13AM +0100, Malcolm Wallace wrote:
> > Yes, -5`div`2 =3D=3D -(5`div`2) =3D=3D -2
> > but (-5)`div`2 =3D=3D -3
> >=20
> > Ghc 5.02.2 has the infix priority wrong, and interprets the former as t=
he latter.
> > But more bizarrely, ghc 5.02.2 gets this very wrong:
> >=20
> > Prelude> (-1796254192) `div` 357566600
> > 5
>=20
> Thanks for the clear summary.
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,
Prelude> 9 `quotRem` (-5)
(-1,4)
(The correct answer is (-1,-4).) I'm frankly astonished: has noone
used these functions with negative arguments before?
This happens with both ghci and compiled programs, both -fvia-C and
-fasm.
Hugs (see below) and nhc make the same error in this case.
There seems to be too strong a reliance on C's operators '/' and '%',
which give these results (with gcc on x86). This may be a bug in
gcc. I'm using gcc 2.95.4; I'm not sure which version compiled the Debian
packages I'm using. According to
http://home.tiscalinet.ch/t_wolf/tw/c/c9x_changes.html#Semantics, the
C9x standard specifies that division should truncate towards 0; I
believe that earlier it was undefined.
I'm shocked that non of the three Haskell implementations had a test
suite that caught this problem.
-----
__ __ __ __ ____ ___ _________________________________________
|| || || || || || ||__ Hugs 98: Based on the Haskell 98 standard
||___|| ||__|| ||__|| __|| Copyright (c) 1994-2001
||---|| ___|| World Wide Web: http://haskell.org/hugs
|| || Report bugs to: hugs-bugs@haskell.org
|| || Version: February 2001 _________________________________________
Haskell 98 mode: Restart with command line option -98 to enable extensions
Reading file "/usr/share/hugs98/lib/Prelude.hs":
Hugs session for:
/usr/share/hugs98/lib/Prelude.hs
Type :? for help
Prelude> 9 `quotRem` (-5)
(-1,4)
-----
--DocE+STaALJfprDB
Content-Type: application/pgp-signature
Content-Disposition: inline
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)
iD8DBQE9Hd7+Veybfhaa3tcRAi4KAJ93NOcEnx5CDdwNSMqdJtXRBA+vfwCeKNDN
Nlv1xbArDIqnpaNCV3FWJWo=
=qQ7j
-----END PGP SIGNATURE-----
--DocE+STaALJfprDB--