久久久久久久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 的意思不同)
主站蜘蛛池模板: 亚洲视频在线看 | 日韩欧美精品在线 | 极品国产视频 | 国产精品久久久久久久久久不蜜臀 | 亚洲一区网站 | 国产成人99av超碰超爽 | 东方伊人免费在线观看 | 成人黄色三级毛片 | 天天久久 | 国产一区二区三区久久久久久久久 | 成人在线播放网站 | 国产伦精品一区二区三区照片91 | 国产精品久久网 | 亚洲精品1 | 日韩av免费在线电影 | 久久国产精品亚洲 | 久久久精品视 | 啪啪av | 国产成人精品久久 | 成人免费在线视频 | 91精品久久久久久久久 | 在线视频 亚洲 | 亚洲视频www | 欧美一区二区三区四区在线 | 日韩美女在线看免费观看 | 午夜精品久久久久久久久久久久 | 久久综合九色综合欧美狠狠 | 国产精品免费一区二区三区四区 | 一级片视频免费观看 | 久国产精品| 欧美日韩综合一区 | 91精品无人区卡一卡二卡三 | 狠狠久久 | 91精品国产综合久久久久久漫画 | 在线中文字幕亚洲 | av一区二区三区四区 | 色嗨嗨| 亚洲精品一区在线 | 污片在线观看 | 成人不卡| 看片国产|