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

python中的負零

negative zero in python(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.

divmodatan2 等操作會表現出這種行為.事實上,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中的負零的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 国产一二三区免费视频 | 久久久做 | 人人干超碰 | www亚洲精品 | 青青草网 | 综合色播 | 久久一区二区三区免费 | 亚洲一区二区中文字幕在线观看 | 天天综合网天天综合色 | 日韩不卡一区二区 | 2019天天干夜夜操 | 国产成人精品一区二 | 亚洲一区在线日韩在线深爱 | 中文字幕亚洲一区 | 一区二区日韩 | 在线a视频网站 | 精品欧美一区二区在线观看欧美熟 | 日韩二三区 | 午夜精品久久久久久久99黑人 | 亚洲精品一区二区三区 | 亚洲一区二区三区在线免费 | 中文在线www | 国产一区二区在线免费视频 | 日本大香伊一区二区三区 | 成人av一区 | 综合久久色 | 国产精品免费一区二区 | 精品少妇一区二区三区日产乱码 | 一区二区三区免费 | av国产在线观看 | 男人天堂网av | 国产伦精品一区二区三区精品视频 | 成人一级视频在线观看 | 亚洲成人午夜电影 | 亚洲不卡视频 | 国产在线视频在线观看 | 日本欧美视频 | 欧美美女爱爱 | a级免费黄色片 | 国产1区| 久久一区二区三区四区 |