comparing vectors return false even they're same

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

script:

if get_slide_count() != 0 :
	var enemy = get_slide_collision(get_slide_count()-1)
	if enemy.collider.is_in_group("enemies"):
		if enemy.normal == Vector2(0, -1):
			enemy_as_floor = true
			print("yes: ", enemy.normal)
		else: print("not: ", enemy.normal)

output when player on the top of a enemy:

yes: (0, -1)
not: (0, -1)
yes: (0, -1)
not: (0, -1)
yes: (0, -1)
not: (0, -1)
yes: (0, -1)
not: (0, -1)
yes: (0, -1)
not: (0, -1)

sometime it also give out:

not: (0, -1)
yes: (0, -1)
not: (0, -1)
not: (0, -1)
not: (0, -1)
not: (0, -1)
not: (0, -1)
yes: (0, -1)
yes: (0, -1)
yes: (0, -1)
yes: (0, -1)

as you see, the enemy.normal is (0, -1) on every outputs, but enemy.normal == Vector2(0, -1) return false? where I do wrong? can anyone explain it

:bust_in_silhouette: Reply From: Zylann

You are likely running into float precision error. It is not good practice to compare vectors this way when physics are involved. print might be rounding that out so it’s hard to see, but it’s there. Your normal is probably something like Vector2(0, 0.99999999) or Vector2(0, 1.0000001).
A better check would then be the following:

if enemy.normal.dot(Vector2(0, -1)) > 0.99

Or

if enemy.normal.distance_to(Vector2(0, -1)) < 0.01