Q: Feature Wish: "%" Extension
Tim Peters
tim.one at home.com
Mon Nov 5 19:49:43 EST 2001
[John Roth]
> The basic sign rules for multiplication are so clear that I don't
> really need to repeat them here. If division is the inverse of
> multiplication, then the sign rules follow directly. Any definition
> of division which does not do that isn't an inverse of multiplication,
> but some other operation masquerading under the same name.
Are you serious? In a world where 3/4 == 0, how long could any programmer
survive believing that integer division and multiplcation are "inverses"?
The antecedent is dead on arrival.
> This might be a much more useful operation in practice, but
> it is confusing to someone who expects the system to behave
> the way he was taught in grammer school.
I can enumerate a hundred ways in which computer arithmetic confuses
beginners, and I bet you could too. It even confuses beginners that, say,
4.00 - 1.00 gets displayed as 3 or 3. instead of 3.00.
> In other words, (-i)/j should -(i/j).
Newbies "know" too that if i < 0 and j > 0, then i/j < 0 (isn't that one of
the "basic sign rules" you're defending?). But-- oops! --*that* one happens
to be true of floor division, but not of truncating division. Integer
division (whether truncating, flooring, rounding, or what have you) is an
information-losing process, and the loss of information kills identities
without prejudice.
I'm not in favor of crippling a language for real-life use just to save a
newbie a few seconds' initial confusion. Besides, newbies are sooooo easy
to confuse no matter what you do <0.5 wink>:
i j i/j (C99 style)
-8 3 -2
-7 3 -2
-6 3 -2
-5 3 -1
-4 3 -1
-3 3 -1
-2 3 0
-1 3 0
0 3 0
1 3 0
2 3 0
3 3 1
4 3 1
5 3 1
6 3 2
7 3 2
8 3 2
Ask your favorite newbie why the quotients -2, -1, 1 and 2 show up 3 times
each, but 0 shows up 5 times. I have asked newbies this. The usual
reaction is paralysis <wink> -- and rote memorization without understanding
is a recipe for disaster in programming work. The table for floor division
is much more comforting, even to a newbie:
i j i/j (Python style)
-9 3 -3
-8 3 -3
-7 3 -3
-6 3 -2
-5 3 -2
-4 3 -2
-3 3 -1
-2 3 -1
-1 3 -1
0 3 0
1 3 0
2 3 0
3 3 1
4 3 1
5 3 1
6 3 2
7 3 2
8 3 2
BTW, the "exactly 3 of each quotient" goes a long way toward explaining why
floor division is less error-prone in practice (for newbies and experts):
the "weird lump" around 0 doesn't exist. Definitions Have Consequences, and
that's a vital lesson for computer newbies to learn. too.
confusion-isn't-to-be-feared-when-it's-a-sign-of-learning-ly y'rs - tim
More information about the Python-list
mailing list