Comparing Vector3 values returns inaccurately

Comparing Vector3 values returns inaccurately in 4.7.2.

I’m comparing Get Actor Location to a stored variable and then setting the variable to Get Actor Location if it’s not matching. Even after setting the variable and with a static actor the result always returns that they don’t match.

This actually seems to happen even if I’m just comparing a float value from the vector. If I print the values to screen they reflect to be the same.

This is something that will happen often when comparing float values

In your case you would be better comparing the distance to a very small number.
(GetActorLocation() - StoredVariableVector).Size() < SmallFloatValue

That is because there are a lot of imprecisions with floats and you can never be sure that the == operator will work.

Just to be safe, try to never compare float values directly as it’ll bring you your share of troubles

Could you compare perhaps up to the first three digits after the decimal?

EG: 123.456789 is the number, I’d like to compare 123.456

Might be worth mentioning I’m using blueprints.

On 4.7 + (tested on 4.7.3 but im pretty sure it came with 4.7)

if I use the (“Vector == Vector” named Equal (Vector) ) node there’s a float value at the bottom of it called error tolerance. You could use that

I did try that with the error tolerance but it still reported inaccurately. I might have to raise the error tolerance a bit. I’ll give it a try and update my post.

You could try to subtract the two of them and print the size of the result to know what kind of tolerance you would need

Adjusting the error tolerance to 0.001 did the trick just fine. Before it was something around 0.00000001 and it was detecting same location as different locations.

Perfect, don’t forget to mark the question as answered for better tracking purpose.

Cheers

  • Marc