在python中如何比较两个float类型的数据是否相等

奇怪的现象

前几天跟同事聊起来,在计算机内部float比较是很坑爹的事情。比方说,0.1+0.2得到的结果竟然不是0.3?

>>> 0.1+0.2
0.30000000000000004

为什么会出现如此傻的结果呢?

这篇文章做了详细的解释,简单的来说就是计算机里面的数字是由二进制保存的,在计算机内部有些数字不能准确的保存,于是就保存一个最靠近的数字。

在十进制中也会存在这样的问题,数字不能准确地表示像1/3这样的数字,所以你必须舍入到0.33之类的东西 - 你不要指望0.33 + 0.33 + 0.33加起来就是1。

因此我们在比较两个float是否相等时,不能仅仅依靠 == 来进行判断,而是当他们两者的差小于一个我们可以容忍的小值时,就可以认为他们就是相等的。

Python中是如何解决的?

各种语言中都有类似的处理方式,python中是这样处理的?
StackOverFlow有类似的问题: what-is-the-best-way-to-compare-floats-for-almost-equality-in-python

简单粗暴的判断方法

return abs(f1 - f2) <= allowed_error

python3.5之后,PEP485提案中已给出了解决方案。
使用math.isclose方法,传入需要比较的两个数和可以接受的精度差值即可。

PEP 485: A function for testing approximate equality

PEP 485 adds the math.isclose() and cmath.isclose() functions which tell whether two values are approximately equal or “close” to each other. Whether or not two values are considered close is determined according to given absolute and relative tolerances. Relative tolerance is the maximum allowed difference between isclose arguments, relative to the larger absolute value:

math.isclose 使用方法

>>> import math
>>> a = 5.0
>>> b = 4.99998
>>> math.isclose(a, b, rel_tol=1e-5)
True
>>> math.isclose(a, b, rel_tol=1e-6)
False
It is also possible to compare two values using absolute tolerance,
 which must be a non-negative value:

>>> import math
>>> a = 5.0
>>> b = 4.99998
>>> math.isclose(a, b, abs_tol=0.00003)
True
>>> math.isclose(a, b, abs_tol=0.00001)
False

转载于:https://www.cnblogs.com/everfight/p/float_equal.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值