Q: Feature Wish: "%" Extension

Jive Dadson jdadson at ix.netcom.com
Sun Nov 4 21:34:05 EST 2001


Paul Rubin wrote:
> 
> "Tim Peters" <tim.one at home.com> writes:
> > > Knuth says to use floor_mod?  Where?
> >
> > Knuth *defines* x mod y as being x - y * floor(x/y), way back in volume 1.
> > It simply has the nicest properties when x and y are ints and y > 0.
> 
> 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".

I could argue that the common understanding of "remainder" is the same
as the mathematical definition of "mod".  If something "remains", it is
positive.

Suppose you have a small business funded by two venture capitalists.

Scenario one:

You have a good month.  After paying creditors, you show a surplus
("remainder") of seven dollars.  You want to divy up the money between
the investors, who insist on getting paid in round amounts.  7//2 = 3
rem 1.  You pay each investor 3 dollars, and you have a remainder of one
dollar.

Scenario two:

You have a bad month.  You are seven bucks in the hole.  To pay your
creditors, you must extract a second round of funding from the
investors.

(-7)//2 = -4 rem 1.

You hit up each investor for four bucks, and after paying your debts,
you have one dollar ("remainder") left over.

Jive



More information about the Python-list mailing list