|Your definition of divFloorRem (and probably divCeilingRem as well)
|doesn't seem to be quite right, because I end up with
|       (-4) `mod` (-3) == -4
|because divTruncateRem (-4) (-3) is (1,-1).
|
|The condition in the "if" should be something like
|       signum r == -signum d
|rather than r<0. (Something like this is in the definition of
|`mod` on page 89 in 1.2beta.)
|
|Stefan Kahrs

Right, thanks.  I've committed the error of forgetting that the
divisor can be negative.  I did the rounding methods first, where
the "divisor" is 1, and then did the division methods by (incorrect!)
analogy.  You'd think that after n years of struggling with with
definitions like these, I'd be able to get them right!

So, as you say, this can be fixed thus:

    divFloorRem n d     =  if signum r == - signum d then (q-1, r+d) else qr
                           where qr@(q,r) = divTruncateRem n d

    divCeilingRem n d   =  if signum r ==   signum d then (q+1, r-d) else qr
                           where qr@(q,r) = divTruncateRem n d

Another way:

    divFloorRem n d     =  if r < 0 then (q-1, r+d) else qr
                           where
                           qr@(q,r) = divTruncateRem n' d'
                           (n', d') = if d < 0 then ((-n),(-d)) else (n,d)

    divCeilingRem n d   =  if r > 0 then (q+1, r-d) else qr
                           where
                           qr@(q,r) = divTruncateRem n' d'
                           (n', d') = if d < 0 then ((-n),(-d)) else (n,d)


--Joe

Reply via email to