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

在 Python 中比較浮點(diǎn)數(shù)是否相等的最佳方法是什么

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

問(wèn)題描述

眾所周知,由于舍入和精度問(wèn)題,比較浮點(diǎn)數(shù)是否相等有點(diǎn)繁瑣.

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 中處理此問(wèn)題的推薦方法是什么?

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

在某個(gè)地方肯定有一個(gè)標(biāo)準(zhǔn)庫(kù)函數(shù)嗎?

Surely there is a standard library function for this somewhere?

推薦答案

Python 3.5 添加了 math.isclosecmath.isclose 函數(shù),如 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 是一個(gè)相對(duì)容差,它乘以兩個(gè)參數(shù)中較大的一個(gè);隨著值變大,它們之間的允許差異也會(huì)變大,同時(shí)仍然認(rèn)為它們相等.

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 是在所有情況下按原樣應(yīng)用的絕對(duì)公差.如果差值小于這些公差中的任何一個(gè),則認(rèn)為這些值相等.

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.

這篇關(guān)于在 Python 中比較浮點(diǎn)數(shù)是否相等的最佳方法是什么?的文章就介紹到這了,希望我們推薦的答案對(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 的意思不同)
主站蜘蛛池模板: 国产91在线播放 | 欧美 日本 国产 | h视频在线播放 | 国产精品国产馆在线真实露脸 | 狠狠av | 精品在线一区 | 午夜小视频在线观看 | 91视频网| 免费久久99精品国产婷婷六月 | 草久视频 | 久久久国产精品视频 | 97视频网站 | 精品欧美乱码久久久久久1区2区 | 欧美久久视频 | 米奇狠狠鲁| 福利精品在线观看 | 美女爽到呻吟久久久久 | 古装人性做爰av网站 | 久久久亚洲一区 | 在线免费观看黄视频 | xxx视频| 国产91在线观看 | 午夜视频一区二区 | 精品国产乱码久久久久久牛牛 | 一区二区三区四区国产 | 日韩在线免费视频 | 丁香五月网久久综合 | 亚洲一区二区日韩 | 国产精品免费播放 | 香蕉视频一区二区 | 成人av网站在线观看 | 麻豆精品久久久 | 日韩国产在线 | 日韩欧美网 | 国产高清在线精品一区二区三区 | 欧美一级片在线观看 | 亚洲综合第一页 | 在线观看中文字幕视频 | 鲁大师一区影视 | 黄色一级电影免费观看 | 欧美黄在线观看 |