div error

Simon Marlow simonmar@microsoft.com
Thu, 6 Jun 2002 13:09:21 +0100


> I did the following check (in ghci):
>=20
>    Prelude> let check a b =3D (a`div`b,a =3D=3D=20
> (a`div`b)*b+(a`mod`b) && ((a`mod`b)>=3D0) && ((a`mod`b)<b))
>    Prelude> check 1 2=20
>    (0,True)
>    Prelude> check (-1) 2
>    (-1,True)
>=20
> Thus, for positive divisors it seems OK.
> However the result for negative divisors I don't understand:
>=20
>    Prelude> check 1 (-2)
>    (-1,False)
>    Prelude> check (-1) (-2)
>    (0,False)

As I understand it, the condition (a`mod`b) >=3D 0 isn't necessarily =
true.
This condition is true, however:

 let check a b =3D let (d,m) =3D divMod a b in=20
 (d, m, a =3D=3D d * b  + m && abs m < abs b && signum m =3D=3D signum =
b)

Cheers,
	Simon