Strange amount of significant digits of floating-point numbers in Godot?

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

Hi community :slight_smile:

With respect to the IEEE Standard for Floating-Point Arithmetic (also known as IEEE 754) there are several types of floating-point numbers like float (32 bit) and double (64 bit) where float is single precision and double is double precision.
The standard mentions that float is able to represent ~7.2 decimal digits and double is able to represent ~15.9 decimal digits. And that is where I’m a little confused about how Godot represents floating-point numbers.

In GDScript I try to compute 1.0/3.0 and print the result with 16 decimal digits to check the amount of significant digits of floating-point numbers in Godot. This is the code of interest:

var result = 1.0/3.0
print("%.16f" % result)

The output of the above code is: 0.3333333333330000

If I try the same computation in Java or C++ I always get:

0.33333334 (in case of float)
0.3333333333333333 (in case of double)

So why does Godot represent floating-point numbers with 12 significant digits? This is not what I would expect with respect to IEEE 754.
Can anyone explain the above result? Or is the print() function the problem?

Here is some data to my OS and the used Godot version:
OS: Windows 8.1, 64 Bit
Godot version: 3.0.2 stable

Thank you :slight_smile:

This could be a bug or a mistake on the engine side, you should probably open an issue on GitHub.

Calinou | 2018-03-06 14:43

Update:

At the moment it seems to be an issue with the print() function. The value of 1.0/3.0is stored correctly internally as a double precision floating-point number with the correct amount of significant digits stated by the IEEE 754 standard.

Here is the link to the issue I opened on GitHub:
Strange/Unexpected amount of significant digits of floating-point numbers in Godot · Issue #17312 · godotengine/godot · GitHub

Foxcataria | 2018-03-06 19:01

The issue has been resolved. Now %f supports up to 16 decimal places.

bluenote | 2019-12-23 09:25