Floating Point Rounding Errors

2025/12/27

Type
Learning Resource
Format
Glossary Article
Version
General
Subject Tags
Code
Assets
All else
Copyright 2016-2026, GDQuest
Created
2026/02/16
Updated
2025/12/27

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 this 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 <.

Become an Indie Gamedev with GDQuest!

Don't stop here. Step-by-step tutorials are fun but they only take you so far.

Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.

Nathan

Founder and teacher at GDQuest
  • Starter Kit
  • Learn Gamedev from Zero
Check out GDSchool

You're welcome in our little community

Get help from peers and pros on GDQuest's Discord server!

20,000 membersJoin Server

Contribute to GDQuest's Free Library

There are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!