[Patches] import floatdivision # 1/2==0.5

David Scherer dscherer@cmu.edu
Tue, 13 Jun 2000 11:45:22 -0400


Skip writes:

> I believe the number one "bug report" category on that list reads
> something like "Python doesn't compare floating point numbers correctly",
> followed by a brief treatise by Tim Peters on the properties of floating
> point arithmetic.  I don't believe I've ever seen a "bug report" that
> suggests there is something wrong with Python's division operation because
> 3/2 == 1 or 2/3 == 0.

Truncating integer division doesn't do anything to ameliorate the problems
with floating point.  The properties which fail to hold for floats also fail
to hold when 1/2 = 0.

I suggest that people who are posting bugs to the bug list are a totally
different population than we are targeting.  Most people have never heard of
truncating division - it really exists only in the programming world.  Randy
Pausch's research, and our own limited experience, strongly suggest that
this is a serious problem.

> It will almost certainly not pass muster with Guido

Well, he called my previous proposal "kind of cute" and even "a fine
starting point" :)

http://www.python.org/pipermail/python-dev/2000-April/004761.html

> will leave you always having to patch Python
> distributions and build those (like Windows) that are commonly distributed
> in binary form.  What will happen if/when your BINARY_FRACTION opcode gets
> used for something else in the 1.7 release?  Then all your pyc
> files will be broken, perhaps in very subtle ways...

We are already committed to providing our own installer on Windows and
Macintosh, because we need to give students a single download.  The big
problem is on Linux, where the Python interpreter is often critical to the
OS.

We also want something integrated with standard Python because we hope that
some of our students will go on to do things other than physics with Python.

Any of the following approaches would work for us, in roughly decreasing
order of preference:
 1. global olddivision (default to 1/2 = 0.5)
 2. pragma floatdivision (same as import, but a new keyword)
 3. import floatdivision
 4. file extension (but this is very fragile)
 5. BINARY_FRACTION opcode only (We do custom compilation in IDLE)

I chose to submit a patch for (3) because it involves no inconvenience to
others, and leaves the possibility of an even better solution open.

> Also, while the patch provides a scoped solution, within a specific scope
> there is no way to undo it (I suppose you could also recognize "import
> intdivision").  What if, after executing "import floatdivision" you wanted
> integer division later on (say, during some loop arithmetic)?  Your patch
> doesn't allow that behavior.

We do not intend to make use of scoped changes at all, though others may.
This is a way of making a language change available to us, without forcing
it on others or breaking library modules.  Changing the semantics of /
constantly would be hideous programming practice.

A purist would argue that there is no such thing as integer division.
Personally, I think int(a/b) is an acceptable way to spell "divide a by b
and truncate" for now, and I would strongly suggest adding "div" and "mod"
operators in the future.

Dave