div 1 (-2) -1 div (-1) 2 -1
but
div (-1) (-2) 0 div 1 2 0 div 0 (-2) 0
Regards, Jerzy Piotrowski CIT UWS, Australia
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) 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
Hi Jerzy, | > div 1 (-2) | -1 | > div (-1) 2 | -1 | | but | > div (-1) (-2) | 0 | > div 1 2 | 0 | > div 0 (-2) | 0 No errors here. Check the Haskell report for properties of div: "The quot, rem, div, and mod class methods satisfy these laws: (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x `quot` is integer division truncated toward zero, while the result of `div` is truncated toward negative infinity." Perhaps you were expecting div to be like quot? All the best, Mark
participants (3)
-
Ch. A. Herrmann -
Jurek Piotrowski -
Mark P Jones