note
Anonymous Monk
Here is my bite into the apple.<br>
<br>
jepri gives us an algorythmn .. that does not use division .. to see how this modulus behaves. Let's use it. :)<br>
<br>
> Actually, if I recall my number theory classes correctly,<br>
> the intent of the modulus operator (x mod y) is to return<br>
> the smallest number possible by repeatedly subtracting<br>
> y from x.<br>
<br>
If we are talking number theory we assume non-negative numbers here. So "smallest number" implies here "smallest integer >= 0". <br>
So.. using Perl integers to emulate natural numbers, we can find the "smallest number" by subtracting until we go negative, and then adding it back on (or storing the number prior to going negative). Let's try:<br>
$x = 10;<br>
$y = 0;<br>
$ans = x;<br>
while($x>=0)<br>
{<br>
$ans -= y;<br>
}<br>
$ans +=y #coming back from the grave<br>
<br>
print "$x % $y = $ans\n";<br>
<br>
Hooray! We are using no division operator, so we *cannot* get a divide by zero error! Running this code has *got* to tell us the real answer, once and for all. :) (I .. er.. don't have a perl interpreter handy just now *cough* could one of you guys run this for me, see what we get? kthx)<br>
<br>
On a different note however, I tested the limit of x%y as y approaches zero from the negative and from the positive. But the limit from either direction appears to be zero.. and does not appear to be X.<br>
<br>
This means that setting "x%y=x where y=0" yields a discontinuity at that point. Yes, I know that modulus yields a discontinuous curve whenever you test x against a constant y.. but aside from this example, it never does that when you test y against a constant x.<br>
<br>
To further visualize this discontinuity, draw a map of x%y for a constant y. It looks like sawteeth, with points y units apart and 45 degree inclines. As you make y smaller and redraw the map, the sawteeth get smaller and closer together. Eventually you come to a rough sandpaper across the horizontal axis of your map. This limits to a horizontal line. But Knuth's exception would have it suddenly jump into a single, boundless 45 degree line.<br>
<br>
I cannot fathom the reasion that a person would want such a function to behave that way. Then again, neither can I fathom why anyone would want to round towards zero in a division-enabled modulo calculation.<br>
<br>
I wonder about "x%0 = 0" though, that at least supports the limits we're seeing. It has a certain beauty to it! x divides evenly into 0 undefined times, undefined * 0 is also undefined -- so the remainder would be undefined - undefined = 0. ;) .. come on, it's ok, you can laugh at math jokes :)<br>
<br>
For serious though, I see about as much utility and symmetry to Knuth's exception as I would to "sin(1/x) where x is zero = planck's constant/bunnies"<br>
<br>
87384
88569
2