Simple quirk in behavior of `mod`
Hello haskell-cafe; I'm fiddling with this<http://cdsmith.wordpress.com/2009/07/20/calculating-multiplicative-inverses-in-modular-arithmetic/>blog post about inverting elements of Z/(p), trying to write the inversion function in pointfree style. This led me to try executing statements like n `mod` 0 which in the ring theoretic sense should be n, at least for integers*. (MathWorld agrees. <http://mathworld.wolfram.com/Congruence.html>) But Hugs gives a division by zero error! I'm more of a recreational haskell user and not too familiar with how the Prelude works. But I dug around a bit and saw this inGHC.Real: ( link<http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Real.html#mod> )
a `mod` b | b == 0 = divZeroError | a == minBound && b == (-1) = overflowError | otherwise = a `modInt` b
Is there a reason why n `mod` 0 is undefined in Haskell? Maybe this has already been considered for Haskell' and I'm just unaware. I did some digging in the archives and this discussion <http://markmail.org/message/5dmehw4lhu56x4zw#query:haskell%20%22%60mod%60%200%22+page:1+mid:7alg3hdlndapyxg6+state:results> <http://markmail.org/message/5dmehw4lhu56x4zw> from 2002 is the most relevant one I could find; it is suggested there that n `mod` 0 should be an error. Thanks all- Nathan Bloomfield *- The mod function is defined in the Integral class, and I'm not even sure how to interpret that. It looks kind of like a Euclidean domain.
Nathan Bloomfield wrote:
Hello haskell-cafe;
I'm fiddling with this <http://cdsmith.wordpress.com/2009/07/20/calculating-multiplicative-inverses-in-modular-arithmetic/> blog post about inverting elements of Z/(p), trying to write the inversion function in pointfree style. This led me to try executing statements like
n `mod` 0
which in the ring theoretic sense should be n, at least for integers*. (MathWorld agrees. <http://mathworld.wolfram.com/Congruence.html>)
I agree that (n `mod` 0) ought to be n. Specifically divMod n 0 = (0,n) and quotRem n 0 = (0,n) In (divMod n m) the sign of the remainder is always the same as the sign of m, unless n or m is zero. In (quotRem n m) the sign of the quotient is the product of the signs of n and m, unless n or m is zero. -- Chris
There are two ways of looking at the mod operator (on integers): 1. As a map from the integers Z to Z/pZ. Then n mod p is defined as: n mod p = { k | k in Z, k = n + ip for some i in Z } Instead of the set, we ususally write its smallest nonnegative element. And yes, in that sense, Z/0Z gives: n mod 0 = { k | k in Z, k = n } = { k } =~ k 2. As the remainder under division by p. Since n mod 0 would be the remainder under division by 0, this correctly gives a division by zero error. I used to think that the definitions were equivalent... apparently not. Thomas On Wed, Jul 22, 2009 at 10:05, Chris Kuklewicz<haskell@list.mightyreason.com> wrote:
Nathan Bloomfield wrote:
Hello haskell-cafe;
I'm fiddling with this <http://cdsmith.wordpress.com/2009/07/20/calculating-multiplicative-inverses-in-modular-arithmetic/> blog post about inverting elements of Z/(p), trying to write the inversion function in pointfree style. This led me to try executing statements like
n `mod` 0
which in the ring theoretic sense should be n, at least for integers*. (MathWorld agrees. <http://mathworld.wolfram.com/Congruence.html>)
I agree that (n `mod` 0) ought to be n. Specifically
divMod n 0 = (0,n)
and
quotRem n 0 = (0,n)
In (divMod n m) the sign of the remainder is always the same as the sign of m, unless n or m is zero. In (quotRem n m) the sign of the quotient is the product of the signs of n and m, unless n or m is zero.
-- Chris
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (3)
-
Chris Kuklewicz -
Nathan Bloomfield -
Thomas ten Cate