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

在 Python 中比較浮點數是否相等的最佳方法是什么

What is the best way to compare floats for almost-equality in Python?(在 Python 中比較浮點數是否相等的最佳方法是什么?)
本文介紹了在 Python 中比較浮點數是否相等的最佳方法是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

眾所周知,由于舍入和精度問題,比較浮點數是否相等有點繁瑣.

It's well known that comparing floats for equality is a little fiddly due to rounding and precision issues.

例如:https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

在 Python 中處理此問題的推薦方法是什么?

What is the recommended way to deal with this in Python?

在某個地方肯定有一個標準庫函數嗎?

Surely there is a standard library function for this somewhere?

推薦答案

Python 3.5 添加了 math.isclosecmath.isclose 函數,如 PEP 485.

Python 3.5 adds the math.isclose and cmath.isclose functions as described in PEP 485.

如果您使用的是早期版本的 Python,則在 文檔.

If you're using an earlier version of Python, the equivalent function is given in the documentation.

def isclose(a, b, rel_tol=1e-09, abs_tol=0.0):
    return abs(a-b) <= max(rel_tol * max(abs(a), abs(b)), abs_tol)

rel_tol 是一個相對容差,它乘以兩個參數中較大的一個;隨著值變大,它們之間的允許差異也會變大,同時仍然認為它們相等.

rel_tol is a relative tolerance, it is multiplied by the greater of the magnitudes of the two arguments; as the values get larger, so does the allowed difference between them while still considering them equal.

abs_tol 是在所有情況下按原樣應用的絕對公差.如果差值小于這些公差中的任何一個,則認為這些值相等.

abs_tol is an absolute tolerance that is applied as-is in all cases. If the difference is less than either of those tolerances, the values are considered equal.

這篇關于在 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.亚洲成人 | 岛国一区二区三区 | 青青草手机在线视频 | 97久久精品人人澡人人爽 | 久草视频观看 | 青青草成人在线 | 天堂网久久 | 天天射天天爽 | 亚洲精品乱码久久久久久蜜桃91 | 日本高潮视频 | 国产一区二区影院 | 成人一区二区三区四区 | 亚洲国产成人精品女人 | 亚洲三区在线 | 亚洲激情中文字幕 | 国产中文字幕一区二区 | 国产美女一区二区三区 | 国产99对白在线播放 | 五月伊人网 | 波多野结衣亚洲一区 | 成人观看视频 | 四虎黄色影院 | 成人伊人网 | 欧美一区二区三区成人 | 在线看亚洲 | 国产天堂网 | 97免费在线视频 | 日韩在线视频观看 | 成人在线视频免费 | 黄av在线| 亚洲欧美日韩一区 | 日韩大片在线观看 | 91理论片午午伦夜理片久久 | 日本免费在线观看视频 | 午夜xxx| 免费日韩视频 | 久久免费看 | 亚洲一区精品视频 | 综合一区二区三区 | 蜜桃精品一区二区三区 |