Addition (bug?)

Hello,
I wrote this simple program:

the result is:
second 1: 0.2
second 2: 0.4
second 3: 0.60000000001
second 4: 0.8

Why second 3 is imperfect ?

Thanks,
J

Because of javascript
image

@4ian you asked me some days ago where this imprecision come.

1 Like

Why don’t you use round? maybe that helps

Round would go to nearest integer. It wouldn’t work for the .20 increments.

I suppose solution is (int(number*10))/10 .

Yeah, it seems to problem is the way computers play with numbers… In case you, or anyone else, need an explanation, here you go.

Imagine you’re in base 3 (so, you have 0, 1, and 2) pick a number, say, 1.1. The number is 1 + 1/3, and we would write that in the base 10 system as 1.333333333 (and so on into infinity)
A similar thing can happen in base 2 that computers use. Sadly, we can’t write an infinite amount of threes (or 1s, in binary) as we don’t have enough space, so it often happens that it’s cut down and the number stored is different from the number calculated by a very, very small number. Like 0.00000000004.

How to fix? Use round(), Gdevelop has the function available!

Toodles!

2 Likes

Hi again, sure I’ve tested round is not accurate, maybe trunc(Value * 10) / 10

1 Like