Re: Illegal Modulus zero
by Zaxo (Archbishop) on May 21, 2005 at 01:43 UTC
|
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.
| [reply] |
|
|
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...
| [reply] |
|
|
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
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
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 :-/
| [reply] [d/l] [select] |
|
|
you missed a '* $modulo', methinks...
Either that, or German schools have really gone downhill :-P
| [reply] |
|
|
| [reply] |
Re: Illegal Modulus zero
by Anonymous Monk on May 21, 2005 at 01:06 UTC
|
Yes nevermind :-) key should be keys
Caught that the 3rd time I checked my code :-) | [reply] |