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


in reply to 0 illegal modulus?

In Mathematics terms, for two numbers A,B to be congruent mod N, then A - B must be an exact multiple of N. So in other words, it is a three argument comparison operator that returns a true/false value, rather than a two argument function like Perl's mod that returns a number between 0 and N-1 (or N+1 and 0 if N is negative).

But Perl's (and other computer languages') mod operator is based on mapping numbers to what mathematicians call fields; in this case a "wrap-around" set of numbers from 0 to N-1 (or from N+1 to 0 if N is negative). In this context, a field with length 0 doesn't make much sense, so is by default an exception.

There are good reasons for this; for instance, much code assumes that if A = B mod N, then abs(A) < abs(N). (abs() being the absolute operator). Knuth's decision that "x mod 0 is defined to be x" may make sense to one school of thought, but to another it's bogus.

If you don't like the behaviour, use a ( $N ? $A % $N : $A ) construct, or see the section in Perl 6 on use limits, which allows you to define the behaviour of this special case.

Update: OK, IANAM and apparently it's only a field when N is prime. But hopefully you can understand what I mean :)