Turn off ZeroDivisionError?

Grant Edwards grante at visi.com
Sun Feb 10 16:55:09 EST 2008


On 2008-02-10, Christian Heimes <lists at cheimes.de> wrote:
> Grant Edwards wrote:
>
>> I've always found that check to be really annoying.  Every
>> time anybody asks about floating point handling, the standard
>> response is that "Python just does whatever the underlying
>> platform does".  Except it doesn't in cases like this. All my
>> platforms do exactly what I want for division by zero: they
>> generate a properly signed INF.  Python chooses to override
>> that (IMO correct) platform behavior with something
>> surprising. Python doesn't generate exceptions for other
>> floating point "events" -- why the inconsistency with divide
>> by zero?
>
> I'm aware result is arguable and professional users may prefer
> +INF for 1/0. However Python does the least surprising thing.

It appears that you and I are surprised by different things.

> It raises an exception because everybody has learned at school
> 1/0 is not allowed.

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).

>>From the PoV of a mathematician Python does the right thing,
>>too. 1/0 is not defined, only the lim(1/x) for x -> 0 is +INF.
>>From the PoV of a numerics guy it's surprising.
>
> Do you suggest that 1./0. results into +INF [1]?

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.

> What should be the result of 1/0?

I don't really care.  An exception is OK with me, but I don't
write code that does integer divide by zero operations.

-- 
Grant Edwards                   grante             Yow!  does your DRESSING
                                  at               ROOM have enough ASPARAGUS?
                               visi.com            



More information about the Python-list mailing list