Problem with variable. Not equal when it seems it should be.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dest

As a testcase ive made this code to show you. The goal is to find the index of the x-coordinate of the vector in the array.
In this code im expecting that testvect[“x”] equals testarray[3], however this is not the case, though the variable prints are the same and the typeof() is the same.
So, i also searching for an awnser why this is not behaving as im expecting it to.

var testarray = [0.1, 0.15, 0.25, 0.45, 0.75, 1.0]
var testvect = Vector2(0.45, 0.45)
print(testarray.find(testvect["x"]))
print(testvect["x"])
print(testarray[3])
print(testvect["x"] == testarray[3])
print(typeof(testvect["x"]))
print(typeof(testarray[3]))
breakpoint

outputs:

-1
0.45
0.45
False
3
3

:bust_in_silhouette: Reply From: volzhs

floating value is approximate.
it is not exactly 0.45.
you need to compare its difference.

if abs(a-b) < 0.0001:
    print("same")
else:
    print("different")

Alright, thanks! I guess i shouldnt use floating point here then, cuz it doesn’t need to be: Ill multiply by 100 XD

dest | 2019-09-27 22:40