http://www.perlmonks.org?node_id=459173


in reply to Illegal Modulus zero

You've found your error but, IMO, "Illegal modulus zero . . ." shouldn't be an error. A number mod zero is perfectly well-defined.

The trick is that '%' doesn't need to be defined in terms of division. It's better defined as,

$z == $x % $y
if $z is a solution of $z == $x - $n * $y for some integer $n
If $y is zero, $x % 0 == $x. If $y is one, $z is the fractional part of $x.

This definition is good for any additive group, since multiplication by an integer has a natural definition there even if multiplication of group elements doesn't exist. That would permit useful overloading of '%' for vectors or other objects with no useful group multiplication.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Illegal Modulus zero
by ivancho (Hermit) on May 21, 2005 at 04:58 UTC
    technically, you'd need some restriction on your z, otherwise z + y is just as good a solution, for n = n - 1. And besides, why would you ever want to look at things mod 0? - if it ever happens, it's probably an error, so might as well report it.

    Also, how do you define the restriction for other additive groups? For vectors you could look at the norm - but that's a consequence of the very very natural and useful multiplication between vectors, ie dot product...

      The restriction in other rings is indeed that z = x % y such that (norm(z) < y -- Update: corrected error spotted by ivancho) norm(z) < norm(y). Of course, you have some freedom in how you define the norm.

      The point is that, if you can define modulus and a norm in such a way, then the ring is a principial ideal ring, and thus, a UFD. (The proof goes this way: norm and remainder => gcd => all ideals are principial => every irreducible elements are prime => unique factorisation.) This is one of the most common proofs you can prove the Fundamental Theorem of arithmetic, that is, the fact that every integer can be decomposed to the product of primes.

      #ifdef MATHMONKS
      #endif
        oooh, heavyweight maths, delicious.. ++

        just want to point out a couple of things - first, a minor ommission, at the top you want norm(z) < norm(y)
        second, I was commenting on the impossibility to define that for general groups - looking at the cyclic group generated by y and its coset generated by x, there's no obvious element to be called 'the remainder'. When you go to rings, things are different, indeed.

        my algebra is a little rusty by now, but I think you need your principal ideal ring to be an integral domain as well, before you get UFD ( ie, no Z_6 and the like ), this relating to your second paragraph.. Looking further down, I see that you talk about domains only, so fair enough, and we don't want to look at ugly non-domain PIRs anyway..

        I think the issue with multivariate polynomials is that they are UFDs, but definitely not PIDs ( e.g. <x,y^2> ), but in any case, now I'm just getting depressed realizing how much I've forgotten, so I'll drop it here, and go back to my leaking recursive closures...

Re^2: Illegal Modulus zero
by holli (Abbot) on May 21, 2005 at 05:05 UTC
    From what I learned in school, modulo is simply defined as: $rest = $number - ($number/$modulo); That given, the error is perfectly justified when $module is 0.

    According to PISA german schools are not the among the best anymore, but in this particular case, perl is with me. ;-)

    Note: This doesn't take into account that perls modulo is an integer modulo.

    Update:
    oops. That should of course be (as ivancho states.): $rest = $number - (($number/$modulo) * $modulo);
    Note to self: Don't post 5 minutes after waking up :-/


    holli, /regexed monk/
      you missed a '* $modulo', methinks...
      Either that, or German schools have really gone downhill :-P
Re^2: Illegal Modulus zero
by ambrus (Abbot) on Sep 25, 2005 at 21:56 UTC