Jan-25-2023, 03:21 PM
You can use numbers like 1e-9 instead of using the pow() function.
def equal(a, b, zero=1e-9): return abs(a-b) < zero print(equal(1.1, 1.1)) print(equal(1.1, 1.1 + 1e-8)) print(equal(1.1, 1.1 + 1e-9)) # sketchy print(equal(1.1, 1.1 + 1e-10))
Output:True
False
False
True
This works fairly well, but what if the numbers you are testing are near zero? If I compare 1e-9 and 1e-9+1e-10 the test will say they are equal even though they differ by 10%. I don't think using a fixed threshold is the correct way to solve this problem.