MLUG: Re: [UUG/MLUG] perl and %
Re: [UUG/MLUG] perl and %
Email address obfuscation in effect -- please click here to turn it off.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
On Thu, 6 Jan 2000, Jonathan King wrote:

> I also question the matlab result, because I really can't see a reason
> why it shouldn't barf on, e.g., mod(12,0) rather than just answer 12
> (which is what it does).

It seems like it should fail, but I can see a rational for mod(x,0)
returning x.  We can define the mod operator so that it doesn't involve
division, only multiplication:  

positive y:  mod(x,y) is the distance from x to the nearest multiple of y
             that is less than or equal to x.
negative y:  mod(x,y) is the distance from x to the nearest multiple of y
             that is greater than or equal to x.
zero y:      mod(x,y) is the distance from x to the nearest multiple of y.

where the distance from a to b is given by a-b and 'nearest' means that
we minimize the absolute value of the distance.

By this definition, when y is zero, mod(x,y) equals x.

Stephen-- What do mathemeticians say is the answer to mod(x,0) where x is
an integer?


> I don't have easy access at this moment to S-plus (or R, it's clone),
> or mathematica, but I'd be stunned if they did it wrong.

I have R.  Here's from the docs:

%% indicates x mod y and %/% indicates integer division. It is guaranteed
that x == (x %% y) + y * ( x %/% y ) unless y == 0 where the result is NA
or NaN (depending on the typeof of the arguments).

Here's the test:

> -98%%100
[1] 2

> -98%%0
[1] NaN


It passes, as Jon predicted.


> So I suppose the lesson here is: Caveat modulor (or something like that).

Stop, you're killing me.


Mike

-- 
Michael B. Miller
University of Missouri--Columbia
http://taxa.psyc.missouri.edu/~mbmiller/