Modulo evaluating incorrectly

Bugs will be moved here once resolved.

Post » Wed Oct 23, 2013 6:34 pm

Link to .capx file (required!):
I'm not allowed to post links so I will try breaking the link up so it is not recognized.
https://
docs.google.com/file/d/0B1keSNvq9c4JZ3VYVFJTZlU4WUE/edit?usp=sharing

Steps to reproduce:
1. run project in debug mode
2. notice that module expression (which outputs to the modulo variable every tick) evaluates incorrectly.

Observed result:
416%(32/5) evaluates to 6.4

Expected result:
416%(32/5) evaluates to 0

Browsers affected:
Chrome: yes
Firefox: yes
Internet Explorer: yes

Operating system & service pack:
Windows 7 Ultimate 64-bit SP1

Construct 2 version:
Release 146 (64-bit/Steam)
B
4
Posts: 13
Reputation: 226

Post » Wed Oct 23, 2013 7:43 pm

More information/context is available in /topic78529 in case it is helpful.
B
4
Posts: 13
Reputation: 226

Post » Thu Oct 24, 2013 11:14 am

The expression is constant so is actually evaluated by the editor at preview/export time.

The editor (like the Javascript engine probably also does) uses double-precision floats for calculations. In this case the expression 32 / 5 evaluates not to 6.4, but to 6.4000000000000004. This is a normal result in computers: they do not perform calculations to infinite precision, and small rounding errors usually occur. It then calculates 416 % 6.4000000000000004 which is 6.3999999999999773. Somehow this gets rounded to 6.4 during export or display, which is unfortunate, but again not unexpected due to the fairly imprecise nature of floating point numbers in computers.

So, closing as won't fix - you can't ever expect floating point calculations to be exactly precise in computers, using any framework or language on any system. You need to make sure you build in tolerances.Ashley2013-10-24 11:14:50
Scirra Founder
B
395
S
232
G
88
Posts: 24,371
Reputation: 193,762

Post » Thu Oct 24, 2013 5:53 pm

Thanks Ashley. Still learning, sorry for taking up your time.
B
4
Posts: 13
Reputation: 226


Return to Closed bugs

Who is online

Users browsing this forum: No registered users and 3 guests