Variable 2.666667 instead of 2.7

Get help using Construct 2

Post » Sun Oct 16, 2016 5:06 pm

Hi Scirra friends,
I have a problem with variables.
I have a variable named "PistolaDaño". Its initial value is 1.2

So, when you press a button (called "Mejorar"), the variable has too add 0.3 to its value.

It means that if press one time that button, the variable will be 1.5.
If i press it twice will be 1.8.
Then 2.1
Etc.

The first 2 or 3 times i do it, everything works well..
BUT,
After some clics its value chage to something like: 2.6999999997 (Instead of 2.7).
(I can see that at the "Bug" test).




You can see it at the pictures:

https://mega.nz/#!owcFyCrQ!eTTeEULLIdf4 ... 7okyORnj6U
https://mega.nz/#!V8lSlRBB!w-Ao6mnpvcZ5 ... ASwXXaj9uw
https://mega.nz/#!t18SnBhR!1Ixbpx9twG7E ... T73DX7vX8g
https://mega.nz/#!wwcBzCAA!ND5hMRmW7UC0 ... KJ82d7L_X8



Thaks all !!!!!!!!
Last edited by GloryF90 on Sun Oct 16, 2016 5:24 pm, edited 1 time in total.
Play my games on www.pocketgames.es
B
13
S
3
Posts: 45
Reputation: 819

Post » Sun Oct 16, 2016 5:21 pm

B
66
S
24
G
7
Posts: 1,515
Reputation: 11,041

Post » Sun Oct 16, 2016 5:25 pm

you cannot expect, when using float numbers, to have a perfect accuracy all the time, due to how computers handle the numbers in memory (0.3 in binary is something equal to 0.01001, which is close to 0.010011001100110011001100110011001100110011001100110011 but not exactly that), so this is actually a bug that is quite innevitable, thus you have to take it in account (by rounding the number accordingly)
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Sun Oct 16, 2016 6:05 pm

mutiply PistolaDaño by 10, round it then divide by 10. Like this:
var PistolaDaño = 1.2;
System->AddTo->0.3
System->SetValue->(round(PistolaDaño * 10))/10


Scirra should add an option to get floats after round and not only ints
Banned User
B
17
S
7
G
24
Posts: 388
Reputation: 14,494

Post » Mon Oct 17, 2016 3:16 am

@GloryF90 Float numbers will always have an error factor
you cold try the following rounding
Code: Select all
round(variable*100)/100
, where 100 is the precision of the number, where 2.699999 becomes 2.70
B
81
S
21
G
23
Posts: 515
Reputation: 18,702

Post » Tue Oct 18, 2016 4:33 pm

Thanks all of you!!
Play my games on www.pocketgames.es
B
13
S
3
Posts: 45
Reputation: 819


Return to How do I....?

Who is online

Users browsing this forum: Apollo14, dop2000, endless409 and 38 guests