为什么python打破IEEE 754为1.0 / 0.0和0.0 / 0.0? [英] Why does python break IEEE 754 for 1.0/0.0 and 0.0/0.0?

查看:82
本文介绍了为什么python打破IEEE 754为1.0 / 0.0和0.0 / 0.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一遍又一遍地读过Python离开浮点

问题直到底层平台。


这似乎基本上是正确的,但不总是。我的基础

平台(IA32 Linux)根据IEEE 754标准正确处理1.0 / 0.0和0.0 / 0.0

,但是Python已经不再使用它了。 b $ b做错事的方法。


1/0由标准定义为+ Inf,0/0是NaN。


这就是我的平台用C语言编写的程序.Python

显然检查除零除了抛出和异常

而不是返回正确的值计算通过

底层平台。


有没有办法让Python为这些操作返回正确的结果

而不是提出异常?


没有办法恢复从异常中返回一个异常处理程序的

值,对吧? [这是IEEE 754标准允许的另一个

选项。]


-

Grant Edwards grante哇! ......我想在电视和振动床上找一个颜色

!!!

visi.com

解决方案

[Grant Edwards]

我一遍又一遍地读到Python将浮点问题留给底层平台。

这似乎基本上是正确的,但并非总是如此。我的基础平台(IA32 Linux)根据IEEE 754标准正确处理1.0 / 0.0和0.0 / 0.0
,但是Python没有采用其做错事的方式。


当除以0时,Python确实会出现提升ZeroDivisionError的问题。

1/0由标准定义为+ Inf和0 / 0是NaN。

这就是我的平台用C语言编写的程序。


IOW,这就是你的平台C的作用(这些案例的行为是由C89标准未定义的,所以不是你可以根据这些结果编写一个_portable_ C89程序的情况。 。

你的平台C返回整数表达式42/0?是否有任何其他

结果错误?

Python显然会检查除零并抛出和异常
而不是返回由<计算的正确值底层平台。

有没有办法让Python为这些操作返回正确的结果而不是引发异常?


否,使用十进制模块时除外。后者提供IBM建议的十进制浮点标准中的所有

设施,

打算成为IEEE 854的超集:

http://www2.hursley.ibm.com/decimal/


在十进制模块中执行此操作相对容易,因为它可以用软件模拟大多数现代FPU提供的所有噱头

硬件。


请注意,当时在Python平台上支持754是很少见的。
Python的设计,没有人在那些日子里提到了754支持,甚至是模糊的

欲望。在没有用户兴趣的情况下,以及在大多数Python平台上缺少对NaN或无穷大的硬件支持,

提出异常的决定是非常明智的时间。

如果没有在大多数平台上模拟SW中的fp算法,Python无法实现754语义(作为十进制模块

今天)并且出于同样的原因,你不能给我早先的一个非愚蠢的

答案你的平台C为整数返回什么

表达式42 / 0"?;今天的问题< wink>。

没有办法恢复从异常中返回一个
值来自异常处理程序,对吧?




正确。


注意这些问题有一个巨大的,最新的,知情的讨论

已经在math.nroot帖子中。


2005-07-14 ,蒂姆·彼得斯< ti ******** @ gmail.com>写道:


除以0时,Python确实会提高ZeroDivisionError。

1/0已定义标准为+ Inf和0/0是NaN。

这就是我的平台为用C编写的程序所做的事。
IOW,那就是你的平台C做的事情(C89标准未对这些案例的行为进行定义,因此,您可以根据这些结果编写一个_portable_ C89程序)。




是的,但是作为一个特殊的事情,我关心的所有C平台都会遵守IEEE 754.

你的平台C为整数表达式返回什么?
42/0?是否有任何其他结果错误?


我想我虽然从我对IEEE 754

的引用中可以明显看出我指的是浮点运算。我不知道(或者通常关心)我的C平台为整数做什么

除以零。

Python显然会检查除零并抛出异常,而不是返回底层平台计算出的正确值。

有没有如何让Python为这些操作返回正确的结果而不是引发异常?



不,除非使用十进制模块。后者提供了IBM建议的十进制浮点标准中的所有功能,它打算成为IEEE 854的超集:

http://www2.hursley.ibm.com/decimal/

注意在Python设计的时候,在Python平台上很少支持754,并且没有人提到754支持,甚至在那些日子里还有一个模糊的愿望。




我经常忘记Python的年龄。不过,我已经在C程序中使用IEEE

浮点数(并且取决于适当的

生成和处理无穷大和NaNs)超过
现在20年了。我以为Python可能已经赶上了。

在没有用户兴趣的情况下,并且在大多数Python平台上没有对NaN或无限的HW支持,


真的吗?我猜想大多数Python平台都是运行Windows或Linux的586或更好的IA32机器。

他们都拥有对NaN和Infinities的硬件支持。 />

时间提出异常的决定是非常明智的。如果没有在大多数平台上模拟SW中的fp算法(如今的
十进制模块那样),Python无法实现754语义,并且出于同样的原因,你无法给出对我之前的对于整数表达式42/0,你的
平台C返回什么?的非愚蠢回答?问题
今天< wink>。

没有办法恢复从异常中返回一个
值来自异常处理程序,对吗?



正确。

请注意,这是一个巨大的,当前的,已经在math.nroot帖子中讨论了这些问题。




-

Grant Edwards grante哇!这是好的---我也是知识产权的



visi.com


< blockquote> [Tim Peters]

....

你的平台C为整数表达式返回什么
42 / 0?是否有任何其他结果错误?


[格兰特爱德华兹]

我想我虽然从我对IEEE 754的引用中可以明显看出我指的是浮点运算。


是的,这很明显。因为我认为我的观点同样显而易见,所以我不会拼写出来< 0.7 wink> ;.


....

请注意,在Python设计的时候,在Python平台上支持754是罕见的,没有人提到754支持,甚至当时的模糊欲望。
我经常忘记Python的年龄。不过,我已经在C程序中使用IEEE
浮点(并且依赖于适当的生成和处理无穷大和NaNs)超过20年。我以为Python可能已经赶上了。


它还没有。请参阅我提到的另一个主题。

如果没有用户的兴趣,并且在大多数Python平台上没有对NaN或无限的HW
支持,
真的吗?


是的,但看起来你还没读完这句话。其余部分是

,重点是:

提出异常的决定在时间上非常明智。




您可能已经忘记了似乎合理的硬件的丰富程度。风景

当时也是。我当时深深地参与了Kendall

Square Research的硬件和SW 754故事的实施,而且这一切都很新颖,很少有先前的艺术绘制为了帮助解决

无数语言问题754没有解决(例如,Fortran的'b
3算法IF语句如果给NaN提供了什么? ?有数百个这样的令人头痛的问题,编译器供应商之间没有合作

,因为语言标准忽略了754)。在C99之前,C标准并没有提到754,然后保留所有支持可选(直到

编译器实现者是否这样做)。这对于一个更大的原因并没有多大帮助:主要的C厂商(如微软和Borland)仍然无视C99而无法支付b $ b。 "子集" 754的硬件实现也很常见,

就像有些根本不支持denorms,有些没有b $ b实现非默认的舍入模式,有些人忽略了签名

零,还有几个通过生成内核来实现754个终端用于处理无穷大和NaN的
陷阱,使得它们慢得多

比正常情况下用户避免使用它们就像死亡一样。


如果我当时不得不下注,我会把钱花在754死亡上

由于几乎普遍缺乏语言支持,并且不兼容

硬件实现。大多数编程语言仍然没有理智的754

的故事,但奔腾架构的显着优势

改变了硬件方面的所有内容。

I我猜想大多数Python平台都是运行Windows或Linux的586或更好的IA32机器。


今天,是的,尽管在许多其他操作系统上仍有Python用户

和架构。大多数后者现在也支持754。

他们都有NaN和Infinities的硬件支持。




是的,基于英特尔的盒子肯定做(并且有很长一段时间),所以

现在做大多数其他人。


I''ve read over and over that Python leaves floating point
issues up to the underlying platform.

This seems to be largely true, but not always. My underlying
platform (IA32 Linux) correctly handles 1.0/0.0 and 0.0/0.0
according to the IEEE 754 standard, but Python goes out of its
way to do the wrong thing.

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

There''s no way to "resume" from the exception and return a
value from an exception handler, right? [This is the other
option allowed by the IEEE 754 standard.]

--
Grant Edwards grante Yow! ... I want a COLOR
at T.V. and a VIBRATING BED!!!
visi.com

解决方案

[Grant Edwards]

I''ve read over and over that Python leaves floating point
issues up to the underlying platform.

This seems to be largely true, but not always. My underlying
platform (IA32 Linux) correctly handles 1.0/0.0 and 0.0/0.0
according to the IEEE 754 standard, but Python goes out of its
way to do the wrong thing.
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). What does
your platform C return for the integer expression 42/0? Is any other
outcome "wrong"?
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. In the absence of user interest, and in the
absence of HW support for NaNs or infinities on most Python platforms,
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.


On 2005-07-14, Tim Peters <ti********@gmail.com> 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


[Tim Peters]
....

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

I guess I though it was obvious from my reference to IEEE 754
that I was referring to floating point operations.
Yes, that was obvious. Since I thought my point would be equally
obvious, I won''t spell it out <0.7 wink>.

....
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.
It has not. Please see the other thread I mentioned.
In the absence of user interest, and in the absence of HW
support for NaNs or infinities on most Python platforms, Really?
Yes, but looks like you didn''t finish reading the sentence. Here''s
the rest, with emphasis added:
the decision to raise an exception was quite sensible AT THE TIME.



You may have forgotten how much richer the "plausible HW" landscape
was at the time too. I was deeply involved in implementing Kendall
Square Research''s HW and SW 754 story at the time, and it was all
quite novel, with little prior art to draw on to help resolve the
myriad language issues 754 didn''t address (e.g., what should Fortran''s
3-branch Arithmetic IF statement do if fed a NaN? there were hundreds
of headaches like that, and no cooperation among compiler vendors
since the language standards ignored 754). The C standards didn''t
mention 754 until C99, and then left all support optional (up to the
compiler implementer whether to do it). That didn''t help much for a
bigger reason: major C vendors (like Microsoft and Borland) are still
ignoring C99. "Subset" HW implementations of 754 were also common,
like some that didn''t support denorms at all, others that didn''t
implement the non-default rounding modes, some that ignored signed
zeroes, and several that implemented 754 endcases by generating kernel
traps to deal with infinities and NaNs, making them so much slower
than normal cases that users avoided them like death.

If I had to bet at the time, I would have put my money on 754 dying
out due to near-universal lack of language support, and incompatible
HW implementations. Most programming languages still have no sane 754
story, but the remarkable dominance of the Pentium architecture
changed everything on the HW side.
I would have guessed that most Python platforms are
''586 or better IA32 machines running either Windows or Linux.
Today, yes, although there are still Python users on many other OSes
and architectures. Most of the latter support 754 too now.
They all have HW support for NaNs and Infinities.



Yes, Intel-based boxes certainly do (and have for a long time), and so
do most others now.


这篇关于为什么python打破IEEE 754为1.0 / 0.0和0.0 / 0.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆