[Python-3000] bug in modulus?

Michael Chermside mcherm at mcherm.com
Wed May 3 13:41:58 CEST 2006


Tim Peters writes:
> IMO, it was a mistake (and partly my fault cuz I didn't whine early)
> for Python to try to define % the same way for ints and floats.
           [...]
> I'd like to see this change in Python 3000.  Note that IBM's proposed
> standard for decimal arithmetic (which Python's "decimal" module
> implements) requires two operations here, one that works like
> math.fmod(a, b) (exact and sign of a), and the other as described
> above (exact and |a%b| <= |b/2|).

Providing both operations is a no-brainer. But only one gets to be
spelled "x % y". Experts will care about sensible behavior of
numerical algorithms... but they're experts, they can use whichever
operation they need. Beginners will care that floats and integers
behave "the same" (okay, that's not actually possible, but beginners
don't know it). So why not make "x % y" for floats behave exactly
like it does for integers and provide a separate operation with your
described behavior?

(Or is that what you were suggesting, and I'm just confused?)

If you're worried about beginners who are implementing numerical
algorithms and hoping to make the language smart enough to "just
work" for them, then I'm afraid it's hopeless. I'm far from a
beginner, but I don't think I could implement a non-trivial
numerical algorithm. (Well, I could make it work, but I'm betting
it would be non-optimal or have corner cases of failure that an
expert could point out.)

-- Michael Chermside



More information about the Python-3000 mailing list