See all glossary terms

Floating Point Rounding Errors

When you try to represent 1/3 as a decimal, you can't represent it precisely with a finite number of digits. You need to write 0.3333.... If you wanted to use that calculation in the real world, you would truncate the number, making it less exact.
1/3 is difficult to represent in a decimal system.
Similarly, computers have a limited number of bits to represent floating-point numbers, so not all real numbers can be represented exactly. Because they use binary, there are different numbers they can't represent well. The decimal value 0.1 cannot be represented exactly as a base 2 fraction. In base 2, 1/10 gives us 0.00011001100110011001100110011001...
Because the result is truncated, it does not convert to 0.1, but to about 0.1000000000000000055511151231257827021181583404541015625.
This leads to some unexpected results.
For example, in most languages, 0.1 + 0.1 + 0.1 does not yield 0.3 but something close. In Javascript, you get 0.30000000000000004 (GDScript does correct and give you 0.3 in this case).
Floating point rounding errors are a common problem in computer science; they're not limited to 0.1, that's just an example.

How to compare floats?

Floating point numbers can't be compared with == or !=, because they're not exact. Instead, we need to use the is_equal_approx() method or is_zero_approx().
It's also good, when possible, to compare with > and <.

See Also

Related terms in the Glossary