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

'is' 運算符對浮點數(shù)的行為異常

#39;is#39; operator behaves unexpectedly with floats(is 運算符對浮點數(shù)的行為異常)
本文介紹了'is' 運算符對浮點數(shù)的行為異常的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在對模塊進行單元測試時遇到了一個令人困惑的問題.該模塊實際上是在轉(zhuǎn)換值,我想比較這些值.

I came across a confusing problem when unit testing a module. The module is actually casting values and I want to compare this values.

==is 相比存在差異(部分,我要注意差異)

There is a difference in comparison with == and is (partly, I'm beware of the difference)

>>> 0.0 is 0.0
True   # as expected
>>> float(0.0) is 0.0
True   # as expected

到目前為止,正如預期的那樣,但這是我的問題":

As expected till now, but here is my "problem":

>>> float(0) is 0.0
False
>>> float(0) is float(0)
False

為什么?至少最后一個讓我很困惑.float(0)float(0.0) 的內(nèi)部表示應(yīng)該相等.與 == 的比較按預期進行.

Why? At least the last one is really confusing to me. The internal representation of float(0) and float(0.0) should be equal. Comparison with == is working as expected.

推薦答案

這與 is 的工作方式有關(guān).它檢查引用而不是值.如果任一參數(shù)分配給同一對象,則返回 True.

This has to do with how is works. It checks for references instead of value. It returns True if either argument is assigned to the same object.

在這種情況下,它們是不同的實例;float(0)float(0) 具有相同的值 ==,但就 Python 而言是不同的實體.CPython 實現(xiàn)還將整數(shù)緩存為此范圍內(nèi)的單例對象 -> [x |x ∈ ? ∧ -5 ≤ x ≤ 256 ]:

In this case, they are different instances; float(0) and float(0) have the same value ==, but are distinct entities as far as Python is concerned. CPython implementation also caches integers as singleton objects in this range -> [x | x ∈ ? ∧ -5 ≤ x ≤ 256 ]:

>>> 0.0 is 0.0
True
>>> float(0) is float(0)  # Not the same reference, unique instances.
False

在這個例子中我們可以演示整數(shù)緩存原理:

In this example we can demonstrate the integer caching principle:

>>> a = 256
>>> b = 256
>>> a is b
True
>>> a = 257
>>> b = 257
>>> a is b
False

現(xiàn)在,如果將浮點數(shù)傳遞給 float(),則簡單地返回浮點字面量(short-circuited),就像使用相同的引用一樣,因為有無需從現(xiàn)有浮點數(shù)實例化新浮點數(shù):

Now, if floats are passed to float(), the float literal is simply returned (short-circuited), as in the same reference is used, as there's no need to instantiate a new float from an existing float:

>>> 0.0 is 0.0
True
>>> float(0.0) is float(0.0)
True

這也可以通過使用 int() 進一步演示:

This can be demonstrated further by using int() also:

>>> int(256.0) is int(256.0)  # Same reference, cached.
True
>>> int(257.0) is int(257.0)  # Different references are returned, not cached.
False
>>> 257 is 257  # Same reference.
True
>>> 257.0 is 257.0  # Same reference. As @Martijn Pieters pointed out.
True

但是,is 的結(jié)果也取決于它正在執(zhí)行的范圍(超出此問題/解釋的范圍),請參考用戶:@Jim 對 代碼對象.甚至 python 的文檔也包含有關(guān)此行??為的部分:

However, the results of is are also dependant on the scope it is being executed in (beyond the span of this question/explanation), please refer to user: @Jim's fantastic explanation on code objects. Even python's doc includes a section on this behavior:

  • 5.9 比較

[7]由于自動垃圾收集、空閑列表和描述符的動態(tài)特性,您可能會注意到 is 運算符的某些使用中看似不尋常的行為,例如涉及實例方法或常量之間比較的那些.查看他們的文檔以獲取更多信息.

[7] Due to automatic garbage-collection, free lists, and the dynamic nature of descriptors, you may notice seemingly unusual behaviour in certain uses of the is operator, like those involving comparisons between instance methods, or constants. Check their documentation for more info.

這篇關(guān)于'is' 運算符對浮點數(shù)的行為異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
Converting Float to Dollars and Cents(將浮點數(shù)轉(zhuǎn)換為美元和美分)
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 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 久久久精品一区二区三区四季av | 国产欧美一区二区三区日本久久久 | 中文字幕高清视频 | av中文字幕在线 | 久久精品国产一区二区 | 中文字幕视频一区二区 | 国产精品日韩一区二区 | 成人免费视频观看视频 | 日韩在线一区二区三区 | 国产精品久久久久久久久久 | 一级片在线观看 | 天天干天天爱天天操 | 日韩精品1区2区3区 成人黄页在线观看 | 色眯眯视频在线观看 | 三极网站| 蜜桃视频在线观看免费视频网站www | 国产欧美精品一区二区色综合朱莉 | 国产高清视频 | 久久精品中文 | 国产网站在线免费观看 | 亚洲视频免费在线观看 | 欧美黄色一区 | 日韩影院在线观看 | 啪啪免费网站 | 日本精品一区二区三区在线观看视频 | 欧美.com| 亚洲成人一区二区三区 | 国产精品欧美一区二区三区 | 国产精品99久久久久久久vr | 美女天天干| 国产精品久久久久久久久久久久午夜片 | 精品三区 | 亚洲一区二区在线视频 | 久久久久亚洲精品 | 日韩一区二区三区在线 | 丁香六月激情 | 国产最新精品视频 | 日韩精品视频在线 | 98成人网| 精品中文字幕在线观看 | 国产精品一区二区三区在线 |