本文介绍了python中的负数为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! python的输出中遇到了负值为零的情况,它的创建例如如下: k = 0.0 print(-k) 输出将是 -0.0 。 但是,当我将 -k 与0.0进行比较时,它会产生True。 (code> 0.0 和 -0.0 是否有区别(我不介意它们大概有不同的内部表示; I只关心他们在一个程序中的行为)有什么隐藏的陷阱我应该知道的? 解决方案 a href =http://en.wikipedia.org/wiki/%E2%88%920_%28number%29 =noreferrer> - 维基百科中的0(数字) 基本上IEEE确实定义了一个负值零 根据这个定义,所有用途: -0.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 >>> 当我为了所有的实际目的说话时,我已经很快地选择了这个词。我的意思是标准的平等比较。 我会在这方面增加更多的信息和参考: $ b $(1)如参考文献所述,IEEE标准将比较定义为使得+0 = -0,而不是-0 像divmod,atan2这样的操作表现出这种行为。事实上,atan2和底层的Clib一样,符合IEEE的定义。定义见参考文献#2。 >>> 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的行为。从讨论中也可以看出,也有微妙的平台变化。 这个方面(IEEE定义合规性)在哪里都没有得到尊重。看到由于不感兴趣而拒绝PEP 754(#3)!我不确定这是否会在稍后被拿起。 引用: http://docs.sun.com/source/806-3568/ncg_goldberg.html#924 FPTAN位于 http://en.wikipedia.org/wiki/Atan2 http://www.python .org / dev / peps / pep-0754 / [Python 3.1]I encountered negative zero in output from python; it's created for example as follows:k = 0.0print(-k)The output will be -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? 解决方案 Check out : −0 (number) in WikipediaBasically IEEE does actually define a negative zeroAnd by this definition for all purposes :-0.0 == +0.0 == 0I 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*d0.0>>> a*c == b*dTrue>>>[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.I would add more information and references in this regard:(1) 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.Operations like divmod, atan2 exhibits this behavior. In fact, atan2 complies with the IEEE definition as does the underlying "C" lib. See reference #2 for definition. >>> 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)FalseOne 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.How ever this aspect(IEEE definition compliance) has not been respected every where. See the rejection of PEP 754 (#3) due to disinterest! I am not sure if this was picked up later.references :http://docs.sun.com/source/806-3568/ncg_goldberg.html#924FPTAN in http://en.wikipedia.org/wiki/Atan2http://www.python.org/dev/peps/pep-0754/ 这篇关于python中的负数为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-04 02:35