Turn off ZeroDivisionError?
Christian Heimes
lists at cheimes.de
Sun Feb 10 17:34:51 EST 2008
Grant Edwards wrote:
> You must have gone to a different school than I did. I learned
> that for IEEE floating point operations a/0. is INF with the
> same sign as a (except when a==0, then you get a NaN).
I'm not talking about CS and IEEE floating point ops. I was referring to
plain good old math. Python targets both newbies and professionals.
That's the reason for two math modules (math and cmath).
> That's certainly what I expected after being told that Python
> doesn't do anything special with floating point operations and
> leaves it all up to the underlying hardware. Quoting from the
> page to linked to, it's also what the IEEE standard specifies:
>
> The IEEE floating-point standard, supported by almost all
> modern processors, specifies that every floating point
> arithmetic operation, including division by zero, has a
> well-defined result. In IEEE 754 arithmetic, a/0 is positive
> infinity when a is positive, negative infinity when a is
> negative, and NaN (not a number) when a = 0.
>
> I was caught completely off guard when I discovered that Python
> goes out of its way to violate that standard, and it resulted
> in my program not working correctly.
Python's a/0 outcome doesn't violate the standards because Python
doesn't promise to follow the IEEE 754 standard in the first place. Mark
and I are working hard to make math in Python more reliable across
platforms. So far we have fixed a lot of problems but we haven't
discussed the a/0 matter.
The best we could give you is an option that makes Python's floats more
IEEE 754 like:
>>> from somemodule import ieee754
>>> with ieee754:
... r = a/0
... print r
inf
Christian
More information about the Python-list
mailing list