如何检查浮点数是否近似整数

Mic*_*per 1 python floating-point

在 Python 中,如何检查浮点数是否近似为整数,并具有用户指定的最大差异?

我在想类似的东西is_whole(f, eps),有f问题的值在哪里,eps是允许的最大偏差,结果如下:

>>> is_whole(1.1, 0.05)
False
>>> is_whole(1.1, 0.2)
True
>>> is_whole(-2.0001, 0.01)
True
Run Code Online (Sandbox Code Playgroud)

Mic*_*per 5

我想出的一个解决方案是

def is_whole(f, eps):
    return abs(f - round(f)) < abs(eps)
Run Code Online (Sandbox Code Playgroud)

但我不确定是否有更 Pythonic 的方法来做到这一点。

编辑

使用abs(eps)而不仅仅是eps为了避免沉默的不当行为。