Behaviour of div & mod with negative arguments?
Hi, Does Haskell specify how div and mod should behave when given one or both arguments negative? Eg, in hugs we get: div 1 3 = 0 div (-1) 3 = -1 div 1 (-3) = -1 div (-1) (-3) = 0 and so on. I've had a bit of a look for where div and mod are specified exactly, but I can only find a definition of their type. Are they defined anywhere? And what is the rational behind the negative arguments part of the definition? Thanks, Mark. P.S. I notice in hugs if I type "-1 `div` 3" the `div` binds to the 1 and 3 first, and only applies the "-" at the end. Is there a reason why the unary "-" has weak binding? -- Dr Mark H Phillips Research Analyst (Mathematician) AUSTRICS - smarter scheduling solutions - www.austrics.com Level 2, 50 Pirie Street, Adelaide SA 5000, Australia Phone +61 8 8226 9850 Fax +61 8 8231 4821 Email mark@austrics.com.au
Dr Mark H Phillips wrote:
Hi,
Does Haskell specify how div and mod should behave when given one or both arguments negative?
Yes, section 6.4.2 gives an exact definition.
P.S. I notice in hugs if I type "-1 `div` 3" the `div` binds to the 1 and 3 first, and only applies the "-" at the end. Is there a reason why the unary "-" has weak binding?
The rational is that unary `-' has the same precedence as binary `-'. Personally, I think this is counter-intuitive to do it the way Haskell does. -- Lennart
participants (2)
-
Dr Mark H Phillips -
Lennart Augustsson