0 Favourites

Float values

  • Link to .capx file (required!):

    www12.zippyshare.com/v/64346451/file.html

    Steps to reproduce:

    1.Set any global value eg. "anyval" to 1.2 (same hapens on 1.3 and 1.7 and many others)

    2.Show str(18*anyval) in text

                       |

         18 or some other values eg. 31, 36

    Observed result:

    I get 21.599999999999998 instand of 21.6 (but if i show str(18*1.2 i get 21.6)

    anyval = 1.2

    18*anyval = 21.599999999999998

    18*1.2 = 21.6

    Expected result:

    anyval = 1.2

    18*anyval = 21.6

    18*1.2 = 21.6

    Browsers affected:

    Chrome: yes

    Firefox: don't have Firefox

    Internet Explorer: yes

    Operating system & service pack:

    Windows 7 64 bit SP 1

    Construct 2 version:

    Beta r123.2

  • Closing. Congratulations, you have learned that computers do not operate at infinite precision; they will have rounding errors. This will happen with any programming language, framework or toolkit you ever use, because the rounding errors happen all the way down at the CPU level.

    For example, calculate (1/3) * 3 to 6 decimal places of precision: you get 0.999999, not 1. That's basically what happens in computer memory, since it can't store an infinite sequence of digits!

    Workaround: only ever compare if a number is within a small range, e.g. abs(a - b) < 0.001.

  • But I still do not understand why if

    anyval = 1.2

    then in c2

    18 * anyval = 21.599999999999998

    though

    18 * 1.2 is 21.6 not 21.599999999999999...

  • Computers work in binary. In binary, lots of numbers are actually recurring fractions (like 0.3333...) when they can be written as a short number in decimal. I checked and 1.2 is one of these numbers that recurs infinitely in binary, and therefore gets truncated.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • OK. But if i try in C2:

    18*1.2 is get me 21.6

    Only if i use global(or other values) i get this 21.599999999999998

    anyval = 1.2

    str((18*(10*anyval))/10) this give me 21.6

  • Whats the point? (pun intended)

    These are tiny values. Its really hard to see how they could be any use in game making.

  • I use layer scale and there is values like 1.2 , 0.7.

  • Check the system expressions.

    There's several ways to format the output to what you need.

  • 18*1.2 is get me 21.6

    Construct 2 is smart enough to calculate constant expressions at export-time with high precision. In other words, if you type 18 * 1.2 in an expression, the game doesn't actually do a multiplication. I guess the editor does this at a higher precision than the runtime which is why it gives the correct answer.

    tr((18*(10*anyval))/10) this give me 21.6

    By multiplying by 10, you go from 1.2 (which requires an infinite number of binary digits) to 12 (which is an integer and can be represented in a finite number of binary digits). So you get a different answer. This is all exactly the same cause as I explained earlier, and will be reproducible in any programming language, framework, engine or computer system whatsoever, unless they deliberately use enhanced precision calculations (such as in financial software).

  • I understand now. But I already did it, differently :)

    Thanks for your help.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)