Adding decimals problem.

  • Hello all.

    This is kind of a bug but It was simple enough to replicate that I didn't want to go through the hassle of reporting it in the bugs section and having to upload a capx.

    The bug.

    Whenever I add 0.1 to an instance variable, the value does not increase by exactly 0.1. Let an instance variable be 0. Add 0.1 and it becomes 0.1, add 0.1 again and it becomes 0.20000000000004, add 0.1 again and it becomes 0.300000000004 and sometimes adding 0.1 again makes the value exactly 0.4 or it continues to have those extra decimals. And after a lot of loops through adding 0.1, the value becomes totally different like 0.3999999999987 instead of 0.4 for example.

    I found this out because I was setting a condition where the instance variable should be equal to 0.9, but it wouldn't fire and after checking in debug I saw that the value was actually 0.899999999999999 instead of 0.9. I don't get how such a thing could happen.

    I tried multiplying and dividing by 10 and also with other objects' instance variables, but the same result.

    Can anyone else replicate this and see if you're getting the same issue?


  • This is not a bug, it's the way floating point math works on computers. You have to just deal with it yourself. Your best option, in using a counter, is to keep it as an integer, and if you need it for some other reason, only divide by 10 at that point. For comparing exact values though, don't do it. Or, you have to deal with comparing a range, not an exact value.

  • Try Construct 3


    Try Now Construct 3 users don't see these ads
  • blackhornet

    Yeah I had to resort to using an extra variable that was incremented by 1 and then divide that by 10 to get exact single decimal place values. I at first resorted to comparing ranges but it seemed so cluttered and annoying. I guess I was taking variables for granted all this while. At least whole numbers should be exact, right? :P

Jump to:
Active Users