+1 vote

When i run this code:

var vec = Vector2(200.714, 201.339)

The output says:

(200.714005, 201.339005)

Can someone explain why this happens?

in Engine by (53 points)

1 Answer

+1 vote

This isn't really a problem with Godot but rather is a general issue when handling decimals in binary. You see, your computer can only handle floating point numbers with so much precision. A number like 200.714 would require infinite precision to represent exactly. See for yourself here. Since floating point numbers can only be represented with finite precision (obviously), the only thing you can do is round to the number that is closest to it that you can represent. So really you can't do anything about that rounding error but to make sure that it doesn't accumulate the error. To learn more about floating point arithmetic visit the Wikipedia.

by (3,891 points)

Then why does it vary like this:

var preciseFloat = 200.71
var vector = Vector2(preciseFloat, preciseFloat)

Gives the output:

(200.710007, 200.710007)

Both the float that I made as a variable and the floats in the Vector2 are supposed to have the same precision. What is happening to the float when it is put in a data type that are supposed to be just two floats? Why does it lose precision?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.