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

Skip Montanaro skip@mojam.com (Skip Montanaro)
Tue, 13 Jun 2000 09:23:59 -0500 (CDT)


    >> Seriously, neither import nor pragma will help newbies make use =
of
    >> '=B7/=B7' ::=3D '=B7/float(=B7)'.

    Dave> We disagree.  At worst, we can simply tell students up front =
that
    Dave> they need to type "import floatdivision" at the beginning of =
every
    Dave> program, because otherwise 3/2 is one, and that leads to
    Dave> hard-to-find bugs.  I don't think it's a difficult habit to l=
earn.

Dave,

My anecdotal experience reading the python-bugs list for awhile is that=

people are thrown for a loop much more often by the imprecision of floa=
ting
point arithmetic than they are by the truncating properties of integer
division.  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 floatin=
g
point arithmetic.  I don't believe I've ever seen a "bug report" that
suggests there is something wrong with Python's division operation beca=
use
3/2 =3D=3D 1 or 2/3 =3D=3D 0.

I will add my vote against the specific patch (I can't recall what the
various values mean - I think the concept is okay but not the mechanism=
).
While I understand the desire to allow floating point results from inte=
ger
division in some contexts and have seen enough traffic on the Python li=
sts
over the years to believe there is a need for it, I think that coopting=
 the
import statement is not the correct way to reach that goal.  It will al=
most
certainly not pass muster with Guido, which (presuming you adopt this c=
hange
in your department) will leave you always having to patch Python
distributions and build those (like Windows) that are commonly distribu=
ted
in binary form.  What will happen if/when your BINARY_FRACTION opcode g=
ets
used for something else in the 1.7 release?  Then all your pyc files wi=
ll be
broken, perhaps in very subtle ways...

Also, while the patch provides a scoped solution, within a specific sco=
pe
there is no way to undo it (I suppose you could also recognize "import
intdivision").  What if, after executing "import floatdivision" you wan=
ted
integer division later on (say, during some loop arithmetic)?  Your pat=
ch
doesn't allow that behavior.  Not being a numerical person, I would fin=
d it
very odd to have to deal with floating point results in loop variables.=

Wouldn't happen a lot, but I'd have to scratch my head whenever it did.=


My recommendation is that for the time being just put up with Marc-Andr=
e's
3/4. and a/float(b) suggestions.  They will always work, and they make =
the
programmer's intent clear.

tim-will-correct-me-if-i'm-wrong-ly y'rs,

--=20
Skip Montanaro, skip@mojam.com, http://www.mojam.com/, http://www.musi-=
cal.com/
On June 24th at 8AM, live your life for an hour as Ricky Byrdsong alway=
s lived
his - run/walk in the Ricky Byrdsong Memorial 5K or just make a donatio=
n:
    https://www.SignmeupSports.com/Events/Index_Events.asp?EventID=3D13=
95