Jan-25-2023, 04:46 PM
(Jan-25-2023, 03:21 PM)deanhystad Wrote: 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))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.
Output:True False False True
I'm trying to write my code from the function def level7_float_comparison(self, a, b):
But I'm baffled why I'm only getting 2 points and not 6/6 points.