Floor of variable set to 1 returns 0

0 favourites
  • 10 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Problem Description

    Adding 0.1 to a variable and displaying it as floor(variable) doesn't return 1 until it's more than 1 (though it works as expected for any other integer)

    EDIT: Having done my floating point homework, I rescind my bug report.

    Attach a Capx

    [attachment=0:1epoa2r5][/attachment:1epoa2r5]

    Description of Capx

    Click the button to add 0.1 to a variable which sets one text using floor and one without.

    Steps to Reproduce Bug

    • Click button 10 times Observed Result floored text still says 0 until 11th click, though 2 happens at 20, 3 at 30, etc.. Expected Result 10 clicks should be 1 Affected Browsers
      • Chrome: (?)
      • FireFox: (YES)
      • Internet Explorer: (?)

    Operating System and Service Pack

    Vista SP2

    Construct 2 Version ID

    173 (apologize if this has already been addressed in newer release)

  • Actually, upon further inspection, 2, 3, and 4 and apparently numbers > 20 seem to work as expected and all other integers do what's described here.

  • Check in the debugger. Your value is actually 0.9999999999999999 but being displayed as 1 in the top textbox.

    Change it to Text: Set text to str(variable1) for much amusement

    Edit: So looks like the 'auto int-to-string' conversion is rounding up..

  • how strange...

    all this time, I thought .1 + .1 + .1 was .3

    math is even harder than I thought!

    (You're right, I'm amused...)

  • how strange...

    all this time, I thought .1 + .1 + .1 was .3

    math is even harder than I thought!

    (You're right, I'm amused...)

    The joys of floating point numbers. A good wakeup call for all of us though

  • So is this the kind of thing that can't really be fixed? Cause it gets ugly fast in some pretty simple situations..

  • There's nothing really to fix, rounding error is inherent. You have to work around the limitations.

    Edit: I haven't worked with much numerical stuff in C2 so I'm not sure all the available facilities.

  • So what you're saying is when it comes down to the final battle between machines and humans, our best defense will be floating point numbers?

  • Yessss.. and viruses..

    Or pose them a paradoxical question... smoke issues forth.. etc.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Closing, see "Expecting math calculations to be exact" in common mis-used events and gotchas.

    In binary floating-point representation, 0.1 is a recurring decimal (like one-third in base 10 is recurring forever as 0.333333333....). Naturally with a limited precision some rounding happens. This happens at the CPU level and affects all software on your system.

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