[Python-ideas] Floating point contexts in Python core

Oscar Benjamin oscar.j.benjamin at gmail.com
Thu Oct 11 20:42:55 CEST 2012


On 11 October 2012 17:05, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Steven D'Aprano writes:
>
>  > I would gladly give up a small amount of speed for better control
>  > over floats, such as whether 1/0.0 raised an exception or
>  > returned infinity.
>
> Isn't that what the fpectl module is supposed to buy, albeit much less
> pleasantly than Decimal contexts do?

But the fpectl module IIUC wouldn't work for 1 / 0. Since Python has
managed to unify integer/float division now it would be a shame to
introduce any new reasons to bring in superfluous .0s again:

with context(zero_division='infinity'):
    x = 1 / 0.0  # float('inf')
    y = 1 / 0  # I'd like to see float('inf') here as well

I've spent 4 hours this week in computer labs with students using
Python 2.7 as an introduction to scientific programming. A significant
portion of that time was spent explaining the int/float division
problem. They all get the issue now but not all of them understand
that it is specifically about division: many are putting .0s
everywhere. I expect it to be easier when we use Python 3 and I can
simply explain that there are two types of division with two different
operators.


Oscar



More information about the Python-ideas mailing list