0 Favourites

Strange Numbers with "&"

  • Link to .capx file (required! If link is blocked remove the http and www parts):

    copy.com/k1YjZyP2VIO4wjMk

    Steps to reproduce:

    1. I have text set to "(Variable1/20)*100&"%"".

    2. Set Variable1 to 11. This is not required if using the capx, as I have it counting down to show all numbers.

    Observed result:

    When it gets to 11, it should show 55%. Instead it shows 55.000000001%.

    However, this only occurs when I have the &"%" added to the end. Not sure why, but without it, it just shows 55. I can get around this by amending "%" to the end... but it's just strange that it only doesn't work with "&" added.

    Expected result:

    55%

    11/20 = 0.55 * 100 = 55. Not sure where the 000000000.1 comes from.

    Tried adding "int" to the beginning, but that doesn't work at all, lol!

    Browsers affected:

    Chrome: yes

    Firefox: yes

    Internet Explorer: yes

    Operating system & service pack:

    Win 7 64bit SP1

    Construct 2 version:

    r159

  • Looks like typical rounding error at floating point math. Rounding the result:

    set text to int((Variable1/20)*100)&"%"

    seems to help.

    Even though I have to admit that I have *no* idea why addition of the &"%" does in any way affect the display format of the float.

    All in all.. Floats do give nasty surprises for unprepared every now and then.

    Edit: yes.. i think this is a bug. Not the 55.000000...001 but the fact that string concatenation (ampresand &) apparently does some rounding.

  • Joannak Strangely enough, when I tried to use int in my main project, I got the results 100%, 0% and -100% only. I just assumed it was doing some major rounding, and took it back out.

    Glad someone's able to reproduce it though.

  • If you start a new empty project and set text object to display (11/20)*100&"%" it works just fine and displays 55%. I didn't take a look at your project. I tested in r159. Do you use any plugins in your project?

  • If you set text to a value like 55.000000001 then it gets rounded and displays "55".

    If you convert the number to a string first (with the str() function or with concatenation) then it will show the number without rounding.

    Set [b]variable1[/b] to [i]11/20 * 100[/i]
    <font color=blue>55.00000000000001</font>
    
    Set text to [i]variable1[/i]
    <font color=blue>"55"</font>
    
    Set text to [i]str(variable1)[/i]
    <font color=blue>"55.00000000000001"</font>
    
    Set text to [i]variable1 & ""[/i]
    <font color=blue>"55.00000000000001"</font>
    

    when I tried to use int in my main project, I got the results 100%, 0% and -100% only.

    Try

    int((Variable1/20)*100) & "%"
  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Try this for a bit of fun:

    capx

    I made it for a help request I put out for similar problems I was having with floating point variables. The bottom line, I suspect, is you can't be care-free when dealing with your variables...

  • nemo I used instance and global variables rather that just (11/20). The instance I use is 11, and the global I use is 20. For some reason, this is what generates the odd number.

    @ramones Thanks, that works, although I still don't understand. I would have thought we wouldn't be having such a strange results appearing... especially after looking at Colludium 's.

  • ramones is right. This is not a bug; computers calculate floating point numbers with limited precision, and in some cases C2 will apply some rounding to try to hide that, but it changes if you use str() or &.

  • ramones is right. This is not a bug; computers calculate floating point numbers with limited precision, and in some cases C2 will apply some rounding to try to hide that, but it changes if you use str() or &.

    Is there a documentation of this automatic rounding? To me (as a long time coder) it seems something that should not be done haphazardly.

    Ok, technically it may save some noobs on making mistakes (like comparing float equality to integer), but any deviations of the normal calculations should be well documented.

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