python中的负零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4083401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:10:22  来源:igfitidea点击:

negative zero in python

pythonfloating-pointfloating-accuracyzero

提问by max

I encountered negative zero in output from python; it's created for example as follows:

我在 python 的输出中遇到了负零;例如,它的创建方式如下:

k = 0.0
print(-k)

The output will be -0.0.

输出将为-0.0.

However, when I compare the -kto 0.0 for equality, it yields True. Is there any difference between 0.0and -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?

但是,当我将 与-k0.0 进行比较时,结果为 True。有什么区别0.0-0.0(我不在乎他们可能有不同的内部表示;我只关心自己的程序的行为。)是否有任何隐藏的陷阱我应该知道的?

采纳答案by pyfunc

Check out ?0 (number) in Wikipedia

在维基百科中查看?0(数字)

Basically IEEE does actually define a negative zero.

基本上 IEEE 确实定义了一个负零。

And by this definition for all purposes:

根据这个定义,出于所有目的:

-0.0 == +0.0 == 0

I agree with aaronasterlingthat -0.0and +0.0are different objects. Making them equal (equality operator) makes sure that subtle bugs are not introduced in the code.
Think of a * b == c * d

我同意aaronasterling-0.0+0.0是不同的对象。使它们相等(相等运算符)确保不会在代码中引入细微的错误。
考虑到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
>>> 


[Edit: More info based on comments]

[编辑:基于评论的更多信息]

When I said for all practical purposes, I had chosen the word rather hastily. I meant standard equality comparison.

当我出于所有实际目的而说这个词时,我相当仓促地选择了这个词。我的意思是标准的平等比较。

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.

正如参考文献所说,IEEE 标准将比较定义为+0 = -0,而不是-0 < +0。尽管总是可以忽略零的符号,但 IEEE 标准并没有这样做。当乘法或除法涉及带符号的零时,通常的符号规则适用于计算答案的符号。

Operations like divmodand atan2exhibit this behavior. In fact, atan2complieswith the IEEE definition as does the underlying "C" lib.

操作喜欢divmodatan2表现出这种行为。事实上,它atan2符合IEEE 定义,底层的“C”库也是如此。

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

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 行为。从讨论中似乎也有微妙的平台变化。

However this aspect (IEEE definition compliance) has not been respected everywhere. See the rejection of PEP 754due to disinterest! I am not sure if this was picked up later.

然而,这方面(IEEE 定义合规性)并未在任何地方得到尊重。请参阅由于不感兴趣而拒绝PEP 754!我不确定这是否是后来捡到的。

See also What Every Computer Scientist Should Know About Floating-Point Arithmetic.

另请参阅每个计算机科学家应该了解的有关浮点运算的知识

回答by Chris Jester-Young

Yes, there is a difference between 0.0 and -0.0 (though Python won't let me reproduce it :-P). If you divide a positive number by 0.0, you get positive infinity; if you divide that same number by -0.0 you get negative infinity.

是的,0.0 和 -0.0 之间是有区别的(尽管 Python 不允许我复制它 :-P)。如果将一个正数除以 0.0,则得到正无穷大;如果将相同的数字除以 -0.0,则会得到负无穷大。

Beyond that, though, there is no practical difference between the two values.

但是,除此之外,这两个值之间没有实际区别。

回答by Craig McQueen

It makes a difference in the atan2()function (at least, in some implementations). In my Python 3.1 and 3.2 on Windows (which is based on the underlying C implementation, according to the note CPython implementation detailnear the bottom of the Python mathmodule documentation):

它在atan2()功能上有所不同(至少,在某些实现中)。在我在 Windows 上的 Python 3.1 和 3.2(基于底层 C 实现,根据Python模块文档底部附近的注释CPython 实现细节):math

>>> import math
>>> math.atan2(0.0, 0.0)
0.0
>>> math.atan2(-0.0, 0.0)
-0.0
>>> math.atan2(0.0, -0.0)
3.141592653589793
>>> math.atan2(-0.0, -0.0)
-3.141592653589793

回答by user

Same values, yet different numbers

相同的值,但不同的数字

>>> Decimal('0').compare(Decimal('-0'))        # Compare value
Decimal('0')                                   # Represents equality

>>> Decimal('0').compare_total(Decimal('-0'))  # Compare using abstract representation
Decimal('1')                                   # Represents a > b

Reference :
http://docs.python.org/2/library/decimal.html#decimal.Decimal.comparehttp://docs.python.org/2/library/decimal.html#decimal.Decimal.compare_total

参考:
http: //docs.python.org/2/library/decimal.html#decimal.Decimal.compare http://docs.python.org/2/library/decimal.html#decimal.Decimal.compare_total

回答by Alex Trebek

math.copysign()treats -0.0and +0.0differently, unless you are running Python on a weird platform:

math.copysign()对待-0.0+0.0不同,除非你在一个奇怪的平台上运行 Python:

math.copysign(x, y)
?????Return xwith the sign of y. On a platform that supports signed zeros, copysign(1.0, -0.0)returns -1.0.

math.复制符号Xÿ
···返回X与符号ÿ。在支持有符号零的平台上,copysign(1.0, -0.0)返回-1.0.

>>> import math
>>> math.copysign(1, -0.0)
-1.0
>>> math.copysign(1, 0.0)
1.0