久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

python中的負(fù)零

negative zero in python(python中的負(fù)零)
本文介紹了python中的負(fù)零的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我在 python 的輸出中遇到負(fù)零;例如,它的創(chuàng)建如下:

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

k = 0.0
print(-k)

輸出將是 -0.0.

但是,當(dāng)我將 -k 與 0.0 進(jìn)行比較時(shí),結(jié)果為 True.0.0-0.0 之間有什么區(qū)別嗎(我不在乎它們可能具有不同的內(nèi)部表示;我只關(guān)心它們?cè)诔绦蛑械男袨?)有什么我應(yīng)該注意的隱藏陷阱嗎?

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(維基百科中的數(shù)字)

基本上 IEEE 確實(shí)定義了一個(gè)負(fù)零.

Basically IEEE does actually define a negative zero.

并且根據(jù)這個(gè)定義用于所有目的:

And by this definition for all purposes:

-0.0 == +0.0 == 0

我同意 aaronasterling -0.0+0.0 是不同的對(duì)象.使它們相等(相等運(yùn)算符)可確保代碼中不會(huì)引入細(xì)微的錯(cuò)誤.
想想 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
>>> 


當(dāng)我出于所有實(shí)際目的說(shuō)這個(gè)詞時(shí),我是相當(dāng)倉(cāng)促地選擇了這個(gè)詞.我的意思是標(biāo)準(zhǔn)的平等比較.

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

正如參考資料所說(shuō),IEEE 標(biāo)準(zhǔn)定義了比較,以便 +0 = -0,而不是 -0 .+0.盡管總是可以忽略零的符號(hào),但 IEEE 標(biāo)準(zhǔn)并沒(méi)有這樣做.當(dāng)乘法或除法涉及帶符號(hào)的零時(shí),通常的符號(hào)規(guī)則適用于計(jì)算答案的符號(hào).

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.

divmodatan2 等操作會(huì)表現(xiàn)出這種行為.事實(shí)上,atan2 符合 IEEE 定義和底層一樣C"庫(kù).

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

一種方法是通過(guò)文檔找出實(shí)現(xiàn)是否符合 IEEE 行為.從討論中似乎也有微妙的平臺(tái)變化.

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 定義合規(guī)性)并未在所有地方得到尊重.請(qǐng)參閱 PEP 754 因不感興趣而被拒絕!我不確定這是不是后來(lái)被撿到的.

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.

另請(qǐng)參閱每個(gè)計(jì)算機(jī)科學(xué)家都應(yīng)該了解的關(guān)于浮動(dòng)的知識(shí)-點(diǎn)算術(shù).

這篇關(guān)于python中的負(fù)零的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點(diǎn)小數(shù)點(diǎn)/精度)
Converting Float to Dollars and Cents(將浮點(diǎn)數(shù)轉(zhuǎn)換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計(jì)算可以返回 NaN?)
Python float to ratio(Python浮動(dòng)比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 99re视频在线 | 国产成人精品一区二 | 国产在线一区二 | 日韩欧美精品 | 亚洲一区二区三区福利 | 91高清视频在线观看 | 欧美一区二区三区四区五区无卡码 | 九久久 | 久久久久久久夜 | 97久久国产| 殴美成人在线视频 | 蜜桃一区二区三区在线 | 亚洲精品成人在线 | 国产一二三区精品视频 | 国产高清在线视频 | 国产精品成av人在线视午夜片 | 日韩看片 | 久久69精品久久久久久久电影好 | 国产精品视频免费播放 | 日韩一二三区视频 | 中文字幕国产精品 | 国产一区黄色 | 91久色 | 亚洲欧美日韩电影 | 不卡的av在线 | 国产精品美女久久久久久久网站 | 欧美理论片在线 | 久久区二区| 欧美日韩国产高清视频 | 夜夜操操操 | 欧美一区二区久久 | 第四色影音先锋 | 久久久精品网 | 韩国精品在线观看 | 亚洲自拍一区在线观看 | 久久在线看 | www久久av | 亚洲高清三级 | 精品自拍视频在线观看 | 久久三区 | 久久久久久久久久久蜜桃 |