negative zero in python(python中的负零)
问题描述
我在 python 的输出中遇到负零;例如,它的创建如下:
I encountered negative zero in output from python; it's created for example as follows:
k = 0.0
print(-k)
输出将是 -0.0
.
但是,当我将 -k
与 0.0 进行比较时,结果为 True.0.0
和 -0.0
之间有什么区别吗(我不在乎它们可能具有不同的内部表示;我只关心它们在程序中的行为.)有什么我应该注意的隐藏陷阱吗?
However, when I compare the -k
to 0.0 for equality, it yields True. Is there any difference between 0.0
and -0.0
(I don't care that they presumably have different internal representation; I only care about their behavior in a program.) Is there any hidden traps I should be aware of?
推荐答案
查看 -0(维基百科中的数字)
基本上 IEEE 确实定义了一个负零.
Basically IEEE does actually define a negative zero.
并且根据这个定义用于所有目的:
And by this definition for all purposes:
-0.0 == +0.0 == 0
我同意 aaronasterling -0.0
和 +0.0
是不同的对象.使它们相等(相等运算符)可确保代码中不会引入细微的错误.
想想 a * b == c * d
I agree with aaronasterling that -0.0
and +0.0
are different objects. Making them equal (equality operator) makes sure that subtle bugs are not introduced in the code.
Think of a * b == c * d
>>> a = 3.4
>>> b =4.4
>>> c = -0.0
>>> d = +0.0
>>> a*c
-0.0
>>> b*d
0.0
>>> a*c == b*d
True
>>>
当我出于所有实际目的说这个词时,我是相当仓促地选择了这个词.我的意思是标准的平等比较.
When I said for all practical purposes, I had chosen the word rather hastily. I meant standard equality comparison.
正如参考资料所说,IEEE 标准定义了比较,以便 +0 = -0
,而不是 -0
.+0代码>.尽管总是可以忽略零的符号,但 IEEE 标准并没有这样做.当乘法或除法涉及带符号的零时,通常的符号规则适用于计算答案的符号.
As the reference says, the IEEE standard defines comparison so that +0 = -0
, rather than -0 < +0
. Although it would be possible always to ignore the sign of zero, the IEEE standard does not do so. When a multiplication or division involves a signed zero, the usual sign rules apply in computing the sign of the answer.
divmod
和 atan2
等操作会表现出这种行为.事实上,atan2
符合 IEEE 定义和底层一样C"库.
Operations like divmod
and atan2
exhibit this behavior. In fact, atan2
complies with the IEEE definition as does the underlying "C" lib.
>>> divmod(-0.0,100)
(-0.0, 0.0)
>>> divmod(+0.0,100)
(0.0, 0.0)
>>> math.atan2(0.0, 0.0) == math.atan2(-0.0, 0.0)
True
>>> math.atan2(0.0, -0.0) == math.atan2(-0.0, -0.0)
False
一种方法是通过文档找出实现是否符合 IEEE 行为.从讨论中似乎也有微妙的平台变化.
One way is to find out through the documentation, if the implementation complies with IEEE behavior . It also seems from the discussion that there are subtle platform variations too.
然而,这一方面(IEEE 定义合规性)并未在所有地方得到尊重.请参阅 PEP 754 因不感兴趣而被拒绝!我不确定这是不是后来被捡到的.
However this aspect (IEEE definition compliance) has not been respected everywhere. See the rejection of PEP 754 due to disinterest! I am not sure if this was picked up later.
另请参阅每个计算机科学家都应该了解的关于浮动的知识-点算术.
这篇关于python中的负零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python中的负零
基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01