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

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

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