Page 1 of 1

Modulo evaluating incorrectly

PostPosted: Wed Oct 23, 2013 6:34 pm
by ravelin
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)

Re: Modulo evaluating incorrectly

PostPosted: Wed Oct 23, 2013 7:43 pm
by ravelin
More information/context is available in /topic78529 in case it is helpful.

Re: Modulo evaluating incorrectly

PostPosted: Thu Oct 24, 2013 11:14 am
by Ashley
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

Re: Modulo evaluating incorrectly

PostPosted: Thu Oct 24, 2013 5:53 pm
by ravelin
Thanks Ashley. Still learning, sorry for taking up your time.