Godot: error when multiply fixed floating number with int coefficient.

Created on 1 Mar 2017  路  9Comments  路  Source: godotengine/godot

Hello,

OS: ArchLinux
Godot version: 2.1.2

When multiply a float value with an int, you get with low values of the coefficient correct results.

And then, everything goes mad: as it's still fixed values for the result, there seems to have overflow somewhere.

Test code (godot -e test.gd):

extends MainLoop
func _initialize():
  for i in range(1,10):
    print("for %d * 5.12 : %f" % [i, i*5.12])

which gives:

for 1 * 5.12 : 5.120000
for 2 * 5.12 : 10.240000
for 3 * 5.12 : 15.360000
for 4 * 5.12 : 20.480000
for 5 * 5.12 : 25.599999
for 6 * 5.12 : 30.719999
for 7 * 5.12 : 35.839999
for 8 * 5.12 : 40.959999
for 9 * 5.12 : 46.079999

So you see the results are correct until coefficient reaches 5.

archived discussion

All 9 comments

Given that Godot 2.x uses floats for floating-point numbers, I see nothing buggy in this (except that a slight bit of rounding might be welcome)

Can you retest with a REAL_T_IS_DOUBLE (not sure if that's default now) build of Godot 3.0/master?

That's not a bug, that's how floating point math works, and there's no fix for it apart from inventing new kind of hardware that could do infinite precision floating point operations :)

The results in straight C are as follows: (format %2.10f)

for float 0 * 5.12: 0.0000000000
for float 1 * 5.12: 5.1199998856
for float 2 * 5.12: 10.2399997711
for float 3 * 5.12: 15.3599996567
for float 4 * 5.12: 20.4799995422
for float 5 * 5.12: 25.6000003815
for float 6 * 5.12: 30.7199993134
for float 7 * 5.12: 35.8400001526
for float 8 * 5.12: 40.9599990845
for float 9 * 5.12: 46.0800018311
for double 0 * 5.12: 0.0000000000
for double 1 * 5.12: 5.1200000000
for double 2 * 5.12: 10.2400000000
for double 3 * 5.12: 15.3600000000
for double 4 * 5.12: 20.4800000000
for double 5 * 5.12: 25.6000000000
for double 6 * 5.12: 30.7200000000
for double 7 * 5.12: 35.8400000000
for double 8 * 5.12: 40.9600000000
for double 9 * 5.12: 46.0800000000

I think this issue should be closed.

@bojidar-bg REAL_T_IS_DOUBLE doesn't actually affect the size of the integer and float types in GDScript. Currently, it's mainly for C++ code under core/math and physics code which use real_t. Once 64-bit migration is finalized, I'll add a scons switch for it.

@hpvb To be precise, I'm not sure why you're seeing that kind of output (compiler flags leading to unsafe math optimizations?), but I'm getting exactly the same output as the OP using float.

It's the output of the following program:

#include <stdio.h>

int main() {
    float f;
    for (int i = 0; i < 10; ++i) {
        f = i * 5.12;
        printf("for float %i * 5.12: %2.8f\n", i, f);
    }

    double d;
    for (int i = 0; i < 10; ++i) {
        d = i * 5.12;
        printf("for double %i * 5.12: %2.8f\n", i, d);
    }
}
for float 0 * 5.12: 0.00000000
for float 1 * 5.12: 5.11999989
for float 2 * 5.12: 10.23999977
for float 3 * 5.12: 15.35999966
for float 4 * 5.12: 20.47999954
for float 5 * 5.12: 25.60000038
for float 6 * 5.12: 30.71999931
for float 7 * 5.12: 35.84000015
for float 8 * 5.12: 40.95999908
for float 9 * 5.12: 46.08000183
for double 0 * 5.12: 0.00000000
for double 1 * 5.12: 5.12000000
for double 2 * 5.12: 10.24000000
for double 3 * 5.12: 15.36000000
for double 4 * 5.12: 20.48000000
for double 5 * 5.12: 25.60000000
for double 6 * 5.12: 30.72000000
for double 7 * 5.12: 35.84000000
for double 8 * 5.12: 40.96000000
for double 9 * 5.12: 46.08000000

I was just trying to illustrate that the 'incorrect' output that OP was seeing wasn't due to any gdscript problem. I'm not sure why the output is a little different but it doesn't really matter for the problem.

So, closing as not a bug.

@hpvb

#include <stdio.h>

int main() {
        float c = 5.12f;
        for(int i=1;i<10;i++)
                printf("%f\n", c*i);
        return 0;
}
5.120000
10.240000
15.360000
20.480000
25.599998
30.719999
35.840000
40.959999
46.079998

The problematic part in your snippet is f = i * 5.12; which is different from f = i * 5.12f.

@akien-mga Sounds good to me.

Was this page helpful?
0 / 5 - 0 ratings