in reply to Modulus Inconsistencies (Or Calling all Mathematicians)
It appears nobody has pointed out yet that use integer will make Perl's mod and division operators work just like C's. More precisely, it will use whatever operations the C compiler that compiled perl happens to like.
So, which is mathematically correct?
It's not a matter of what is correct. In math, we get to make up anything we want, and then reason from it. If you don't believe me, you just haven't studied enough higher math. What is important is whether it is consistent and useful. "Division" means that you're given x and d, and you find q and r such that
x = q * d + r
Normally, we like r positive, but that's a matter of convention. As long as the / operator gives you q and % gives you r that work in this equation, things are consistent. All C compilers I know of follow this rule. Perl's division operator doesn't quite follow it, because it returns a floating-point result. You need to do a floor (not int!) after division to get the right result in integer arithmetic.
As to what's useful, when writing number-theoretic algorithms, it is almost always preferable to have the modulus give you a result that's always positive, like Perl's mod operator does.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Modulus Inconsistencies (Or Calling all Mathematicians)
by bronto (Priest) on Mar 26, 2003 at 18:13 UTC | |
by no_slogan (Deacon) on Mar 26, 2003 at 18:37 UTC | |
|
Re: Re: Modulus Inconsistencies (Or Calling all Mathematicians)
by Helter (Chaplain) on Mar 26, 2003 at 19:02 UTC | |
by no_slogan (Deacon) on Mar 26, 2003 at 19:30 UTC | |
by dragonchild (Archbishop) on Mar 26, 2003 at 19:24 UTC |