Negative Mods Revisited

A few blogs back, I noted that Excel and C# differ when negative numbers are given to the "mod" function. The language Haskell has support for BOTH styles, and the clarity is refreshing. Haskell has one pair, 'div' and 'mod', which truncate toward 0 (the Excel style), and another pair, 'quot' and 'rem', which truncate toward to -Infinity (the C# style).  I had blogged that both styles were acceptable, and it's nice to see some validation of that opinion. Check this out (pasted out from a Hugs98 session -- see Haskell link above)

Main> zipWith mod [30, -30, 30, -30] [21, 21, -21, -21]
[9,12,-12,-9] :: [Integer]
(307 reductions, 472 cells)
Main> zipWith rem [30, -30, 30, -30] [21, 21, -21, -21]
[9,-9,9,-9] :: [Integer]
(99 reductions, 172 cells)

Published Friday, August 20, 2004 5:27 PM by brianbec

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)