PEP0238 lament

Tim Hochberg tim.hochberg at ieee.org
Mon Jul 23 12:07:45 EDT 2001


"Tim Peters" <tim.one at home.com> wrote in message

[SNIP]
> Indeed, nobody with a keen interest in
> numerics has bugged Guido about this except Moshe, that I know of; the
most
> visible repeated pressures have come from educators using Python as a
means
> to some other end.
[SNIP]

Hey, I'm one of those people with a keen interest in numerics who hasn't
bugged Guido about this yet. Let me rectify the situation. First off a
disclaimer, I don't claim to be a hard core numerical analysist of any
sort -- I'm an engineer of sorts and I know just enough numerical analysis
to get myself into trouble. I do use python almost entirely for numerical
work and have been using it since way back in version 0.9.2 or so.

Let me stray from the topic for a paragraph here: I'm astounded to discover
the number of people pining for integer division that truncates towards
zero. That sort of integer division is horriible. When programming in C,  I
always had to simulate Python's behaviour in order to get code that might
take both positive and negative integral values to work correctly. Python
gets this exactly right.

Back to the topic, more or less: I've been astounded by way the arguments
against this has played out. I expected the opposition to say more or less,
"It's a small problem at worst, and it will break 10 zillion lines of code,
and will be a huge pain to upgrade, so it's just not worth it". Instead, the
talk of code breakage has been suprisingly small and there seems to be a
fair number of people claiming that the current mixed behaviour is a _good_
thing. I couldn't disagree more. C-style division in a dynamically typed
language is an accident waiting to happen. And does this mean that code
breakage won't be as large and/or painful as I thought it might be? It also
seems that a lot of the people most vociferously opposed to the PEP have
very little idea what's actually in it. In particuluar, there seems to be
many out there who seem horrifed at the prospect of integer division going
away even though this was never proposed.

Anyway, not being an educator, I'm not particularly concerned with whether
integer division is easy or hard to learn. I like the proposal because there
just isn't a good way currently to spell
divide-these-two -numbers-using-normal-division. The fact that oponents of
this proposal keep suggesting methods for doing this that are wrong only
reveals that this is more difficult than generally supposed.

Consider the completely unsafe function:

def simple_func(x, y):
   return y / x**2

It has been suggested as recently as this morning, I believe, that this be
rewritten:

def simple_func(x, y):
   return y / float(x)**2

This is simple, clear and ... wrong, as Konrad Hinsen pointed out several
days ago. At least it's wrong if complex numbers are ever a possibility. One
could instead write it using complex(x), but one generally does not want to
convert to a complex number unless the inputs are complex, so that solution
is unsatisfactory. A more correct solution suggested by Konrad is:

def simple_func(x, y):
   return y / (x+0.0)**2

This is correct, but can hardly be called simple or clear. Also, in the
context of Numeric Python, where x and/or y may be an array, the above code
may be inefficient in time and extremely inefficient in space. It's possible
of course to work around this, but it becomes even less simple and clear.

Time to stop before I get too inflamatory,

-tim








More information about the Python-list mailing list