Comparing same vector or floats values returns false

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

Hello!

Comparing two vectors with the same coordinates returns false.

Waypoint is a vector 3 in this scenario.

self.get_translation() == waypoint

returns false although this these are the coordinates given by the output:

(1.05922, 12.412035, -0.650758) < waypoint
(1.05922, 12.412035, -0.650758) < self.get_translation()

Then i tried to check the state via the distance and tried

var distance = self.get_translation().distance_to(waypoint) == 0
distance == 0.0

Same problem.

print distance 

shows 0

But now comes the funny part. Both parts work at the beginning. They return true when they should.

But after a while (reproducable at the same time) they return false.

Thanks for your time!

:bust_in_silhouette: Reply From: MysteryGM

Because of floating point errors, two floats could look similar but have a very small difference.

Instead of If Vector == Vector try if Vector > Vector
This also solves other floating point errors like 1/3.

Thanks for the tip! I’m now working with the distance and an adjustable tolerance value. Everything works fine now!

if abs(self.get_translation().distance_to(waypoint)) < tolerance

AirbornePanda | 2018-08-30 22:30