Q: Feature Wish: "%" Extension

Tim Peters tim.one at home.com
Sun Nov 4 18:27:19 EST 2001


[Paul Rubin]
> Knuth says to use floor_mod?  Where?

[Tim]
> Knuth *defines* x mod y as being x - y * floor(x/y), way back
> in volume 1.

[Paul]
> Yes, x mod y is usually defined that way.  However, "x remainder y"
> doesn't necessarily mean the same thing.  The question then is whether
> % in a programming language should denote "mod" or "remainder".

The question was "what does Knuth say?" -- as you just quoted yourself
asking <wink>.  Luckily, the answer to that turns out to be the same as the
answer to what % in a programming like should do:  a negative modulus is
exceedingly rare in real life (I believe I've never used one, apart from
writing compiler conformance tests), and floor-mod is gobs more useful for
positive modulus.  Some languages supply more than one operator.

> ...
> All integer division instructions I've seen give separate quotient
> and remainder results, with remainder==trunc_mod.

I agree that's most common.  The RISC trend is to supply no integer division
instruction at all; Intel's Itanium is a prominent current example:

<http://developer.intel.com/software/products/opensource/libraries/numnote2.
htm>

Why do it in hardware when you can explain how to fake it in a 131-page
document <wink>?





More information about the Python-list mailing list