I need to compare floating point numbers with inequalities:
if x >= y:
However, due to floating point precision issues, sometimes this fails when it should succeed (x = 0.21 and y= 0.21000000000000002). I was thinking to create an epsilon:
epsilon = 0.000000001
if x >= y - epsilon:
I'd rather use a standard mechanism for this. Python has a math.isclose function that works for equality, but I couldn't find anything for inequality. So I have to write something like this:
import math
if x > y or math.isclose(x, y):
I have to do this a ton in my application... easy enough, I'll just create a function. My question is if there's a standard way to deal with inequalities and floats? Is there a numpy.greater_or_equal(x, y) type function?
Since you indicated that the overhead of a function call is not really an issue, why not simply provide:
You can import that wherever you need it.
Your comments stated: "This seems like one of those functions that I can get wrong easily due to surprising floating point precision issues", but since this only relies on
xactually being greater thany, or using themath.isclose()function, that risks seems to be absent?(or rather, keeping the import outside the function:)