Re: Why does python break IEEE 754 for 1.0/0.0 and 0.0/0.0?



On 2005-07-14, Tim Peters <tim.peters@xxxxxxxxx> wrote:

> Python does go out of its way to raise ZeroDivisionError when
> dividing by 0.
>
>> 1/0 is defined by the standard as +Inf and 0/0 is NaN.
>>
>> That's what my platform does for programs written in C.
>
> IOW, that's what your platform C does (the behavior of these
> cases is left undefined by the C89 standard, so it's not the
> case that you can write a _portable_ C89 program relying on
> these outcomes).

True, but as a paracial matter, all of the C platforms I care
about all do obey IEEE 754.

> What does your platform C return for the integer expression
> 42/0? Is any other outcome "wrong"?

I guess I though it was obvious from my reference to IEEE 754
that I was referring to floating point operations. I don't
know (or generally care) what my C platform does for integer
divide by zero.

>> Python apparently checks for division by zero and throws and
>> exception rather than returning the correct value calculated
>> by the underlying platform.
>>
>> Is there any way to get Python to return the correct results
>> for those operations rather than raising an exception?
>
> No, except when using the decimal module. The latter provides
> all the facilities in IBM's proposed standard for decimal
> floating-point, which intends to be a superset of IEEE 854:
>
> http://www2.hursley.ibm.com/decimal/
>
> It's relatively easy to do this in the decimal module because
> it emulates, in software, all the gimmicks that most modern
> FPUs provide in hardware.
>
> Note that support for 754 was rare on Python platforms at the
> time Python was designed, and nobody mentioned 754 support as
> even a vague desire in those days.

I often foget how old Python is. Still, I've been using IEEE
floating point in C programs (and depending on the proper
production and handling of infinities and NaNs) for more than
20 years now. I had thought that Python might have caught up.

> In the absence of user interest, and in the absence of HW
> support for NaNs or infinities on most Python platforms,

Really? I would have guessed that most Python platforms are
'586 or better IA32 machines running either Windows or Linux.
They all have HW support for NaNs and Infinities.

> the decision to raise an exception was quite sensible at the
> time. Python could not have implemented 754 semantics without
> doing emulating fp arithmetic in SW on most platforms (as the
> decimal module does today), and for much the same reasons you
> can't give a non-silly answer to my earlier "what does your
> platform C return for the integer expression 42/0?" question
> today <wink>.
>
>> There's no way to "resume" from the exception and return a
>> value from an exception handler, right?
>
> Correct.
>
> Note that there's a huge, current, informed discussion of
> these issues already in the math.nroot thread.

--
Grant Edwards grante Yow! It's OKAY --- I'm an
at INTELLECTUAL, too.
visi.com
.



Relevant Pages

  • Re: math.nroot [was Re: A brief question.]
    ... settable traps and flags and rounding modes and all that jazz. ... Maybe we should just implement floats in Python. ... Your platform C may or may not, and your OS may or may not. ... I only really care about platforms that make at least a half-hearted ...
    (comp.lang.python)
  • Re: math.nroot [was Re: A brief question.]
    ... >>> using the platform libm or Python needing to write its own libm. ... > It's entirely optional part of C99. ...
    (comp.lang.python)
  • Re: math.nroot [was Re: A brief question.]
    ... > Maybe we should just implement floats in Python. ... Your platform C may or may not, and your OS may or may not. ... >> I believe Python should raise exceptions in these cases by default, ... trying to _increase_ libm error coverage, it was trying to get back ...
    (comp.lang.python)
  • Re: Why does python break IEEE 754 for 1.0/0.0 and 0.0/0.0?
    ... > issues up to the underlying platform. ... > according to the IEEE 754 standard, but Python goes out of its ... > Python apparently checks for division by zero and throws and exception ...
    (comp.lang.python)
  • Re: How to delete a feature type in PB4.2
    ... > My platform(Sitsang: a reference board of PXA250) are difficult to start. ... > Setting the "Exclude from build" flag on the PCMCIA driver for ... > the necessary support is not present in your platform. ... > Setting the "Exclude from build" flag on the Local Area Networking ...
    (microsoft.public.windowsce.platbuilder)