Floor of variable set to 1 returns 0

Bugs will be moved here once resolved.

Post » Tue Aug 19, 2014 1:46 am

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
floor.capx


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)
You do not have the required permissions to view the files attached to this post.
Last edited by spacedoubt on Tue Aug 19, 2014 4:26 am, edited 1 time in total.
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Tue Aug 19, 2014 1:48 am

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.
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Tue Aug 19, 2014 1:59 am

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..
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 19, 2014 2:07 am

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...)
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Tue Aug 19, 2014 2:11 am

spacedoubt wrote: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 :)
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 19, 2014 2:20 am

So is this the kind of thing that can't really be fixed? Cause it gets ugly fast in some pretty simple situations..
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Tue Aug 19, 2014 2:30 am

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.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 19, 2014 2:40 am

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?
B
86
S
43
G
71
Posts: 601
Reputation: 43,619

Post » Tue Aug 19, 2014 2:45 am

Yessss.. and viruses..

Or pose them a paradoxical question... smoke issues forth.. etc.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 19, 2014 12:08 pm

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.
Scirra Founder
B
395
S
232
G
88
Posts: 24,368
Reputation: 193,746


Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 2 guests