Problem when adding "x" to Text displaying a Var

0 favourites
  • 12 posts
From the Asset Store
Change delay, create new lines, "backspace" the text
  • Hi. I have encountered a strange problem when adding "0.1" to a global var. The frist time it adds 0.1 to 1 it shows "1.1" as expected. But the second time the value becomes "1.2000000000000002" and then "1.3000000000000003" and so on...

    The Number should be shown in the UI as "Multiplier: Yx" where Y is the variable which gets incremented.

    After experimenting a bit I found this: When I don't add "& "x"" in the text object it works and adds only 0.1 everytime. But when I add a "x" in the textobject behind the number I get this very long numbers. But why?

    Adding an "x" letter to the text object should not change or affect the variable?

    <img src="http://img5.fotos-hochladen.net/uploads/multiplierworki8ph54kr3l0.jpg" border="0" />

  • It doesn't change the value, it just doesn't display it in the text when the value isn't converted to a string for the text, maybe because some rounding is in place exactly for those cases. Check out the debugger to see that the value of the variable is indeed this bandworm of a number, no matter how it is displayed.

    I know there are some very small computational inaccuracies in Construct, but I didn't think it would happen for additions :/ sucks a bit.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I created an example. When I add "x" as string behind the number the number gets wrong, when I just display the variable (a number) it works fine. It's a bug? <img src="smileys/smiley5.gif" border="0" align="middle" />

    dl.dropboxusercontent.com/u/26714455/IncrementExample.capx

    But the lower part without the "x" shows the short number 1.2 even if the debugger says the var contains "1.2000000000000002".

  • It doesn't change the value, it just doesn't display it in the text when the value isn't converted to a string for the text, maybe because some rounding is in place exactly for those cases. Check out the debugger to see that the value of the variable is indeed this bandworm of a number, no matter how it is displayed.

    I know there are some very small computational inaccuracies in Construct, but I didn't think it would happen for additions :/ sucks a bit.

    mindfaQ

    It is not C2's fault, it is how computers work, because a decimal number, when converted to binary, has not always an ended number of decimals (Maybe there are also other problems in top of this), and so, they are rounded in the process, either way, try to keep that in mind when using a softtware that uses floats

  • The correct answer is probably "1.2000000000000002" - computers have a limited precision so rounding errors are common. However in some circumstances C2 will slightly round values to try and hide these rounding errors. (In retrospect, I'm not sure if that's such a good idea...)

    Imagine doing one third times three with 8 digits of precision. That means you round one third to 0.33333333. Times three is 0.99999999. Lo and behold, one third times three is not 1. The same thing happens in binary with numbers like 0.1, which are actually have a recurring representation in binary.

  • Aphrodite: yes I guess so, I just didn't expect it to happen for +0.1.

    Bl4ckSh33p: I have already explained it. Conversion to string leads to the different display. There is probably a display rule in place that prevents those little differences to show when the number is still handled as float. If you use str(muliplier) in your text, you will see all decimals and thats also what happens when you append the &"x".

  • I see, thanks. But is it possible to use the "short" number as string somehow? I tried "floor" and "int" but this only displays whole numbers (1, 2, 3) and no decimals.

  • Bl4ckSh33p,

    Here's a potential workaround for you. This will give you an answer to one decimal place (adjust as you require):

    string = 0.1 * round(YourNumber * 10)

  • Thank you very much.

    I added your example but it is still not working everytime when I add "& "x"" behind it. 1.1 is shown, 1.2 is not, 1.3 is shown, 1.4 not and so on.

    It seems C2 does not like to add a string behind it. <img src="smileys/smiley6.gif" border="0" align="middle" />

    When I don't add the string "x" it seems to work every time.

  • That's weird! I have just duplicated this - it doesn't make sense although it seems to be something to do with having the global variable at the start of the display text string. If you make your text string look like this then it'll display correctly (ie have some text to display first, even if it's a nil entry):

    "" & Multiplier & "x"

  • Right! It works. Even without rounding and the many 00000's in the var according to the debugger, it displays only 1.x, 2.x and so on.

    Thank you very much to everyone who took the time to read and reply. <img src="smileys/smiley1.gif" border="0" align="middle" />

    Now I only have to fix this problem with selecting and deleting family members. (http://www.scirra.com/forum/topic82615.html)

  • I've submitted a bug report for this.

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