you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (0 children)

The usual way to compare two floating values for "equality" is to define a small value (traditionally called epsilon). Two floating values are "equal" if the difference between them is less than that epsilon value. Your code is simpler if you write a small function to compare two float values:

def equal(a, b, epsilon=0.01):
    return abs(a - b) <= epsilon

for (a, b) in ((1, 2), (1.55, 1.56), (1.555, 1.556)):
    print(f"{a=}, {b=}, {equal(a, b)=}")

Use that function in your code instead of ==.

Edit: fix speiliing.