div error

Ch. A. Herrmann herrmann@fmi.uni-passau.de
Thu, 6 Jun 2002 09:59:32 +0200


Hi,

    >> div 1 (-2)
    Jurek> -1

   >> div 1 2
    Jurek> 0

this is no contradiction since integer division
is not symmetric for positive and negative numbers.

I did the following check (in ghci):

   Prelude> let check a b = (a`div`b,a == (a`div`b)*b+(a`mod`b) && ((a`mod`b)>=0) && ((a`mod`b)<b))
   Prelude> check 1 2 
   (0,True)
   Prelude> check (-1) 2
   (-1,True)

Thus, for positive divisors it seems OK.
However the result for negative divisors I don't understand:

   Prelude> check 1 (-2)
   (-1,False)
   Prelude> check (-1) (-2)
   (0,False)

Cheers
--
 Christoph