zoukankan      html  css  js  c++  java
  • 在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
    
  • 相关阅读:
    如何在DBGrid中能支持多项记录的选择
    How to create a OnCellDblClick for Delphi's TDBGrid
    如何在DBGrid里实现Shift+“选择行”区间多选的功能!
    DBGrid中Shift多选
    代码校验工具 SublimeLinter 的安装与使用
    jquery压缩图片插件
    React 入门最好的实例-TodoList
    前端切图
    提升前端效率的方式
    单页面应用的痛点
  • 原文地址:https://www.cnblogs.com/everfight/p/float_equal.html
Copyright © 2011-2022 走看看