[Numpy-discussion] Re: numpy, overflow, inf, ieee, and rich , comparison

Johann Hibschman johann at physics.berkeley.edu
Fri Oct 27 19:49:07 EDT 2000


Darren New writes:

> Johann Hibschman wrote:
>> What's 'remainder', if not the same as 'modulo'?  

> The problem is with negatives. X mod 7 is between 0 and 6 (inclusive) for
> all X.

> -3 mod 7 != 3 and -3 mod 7 != -3.

> -3 mod 7 = 4. -1 mod 7 = 6.

Oh, okay.  I thought that was a given for any mod/remainder function.
So there's no problem with the python implementation, then?  I thought
you were implying that there was.

> That's the definition of mod, and that's the semantics I find useful.
> Note: I don't think there *is* a definition for mod that handles a negative
> number as the second argument.

Well, there's the least positive residue that I mentioned before; I'm
used to seeing that one.  Congruence modulo m is defined for negative
m, i.e. a = b mod m iff m divides (a-b).  So all you have to do is
pick which congruent number you want, and the "least positive" rule
works for that.

Python seems to pick the "least negative" when m < 0, which is kinda
weird, but at least consistent.

>> "r = a % m" should preferrably be in the range from 0 < r < |m|,
>> right?

> Well, 0 <= r < |m| I would guess.

Right.  Oops.

(I've gotta avoid getting drawn into these math discussions, or I'll
have to invoke Sarah again, and Ping will mock me.)

-- 
Johann Hibschman                           johann at physics.berkeley.edu



More information about the Python-list mailing list