A mathematical bug

  • A mathematical bug

    Sometimes the software calculates decimal numbers incorrectly.

    For example, instead of 0.2 it calculates 0.1999999, and sometimes 0.200000001.

    I do not know why this happens, but it is repetitive.

    Attach a Capx

    https://googledrive.com/host/0B5et6AUuw ... 20Bug.capx

    Description of Capx

    It is a very short CAPX, so here's a picture:

    Steps to Reproduce Bug

    Just run the program with the debugger running, and see the bug.

    Observed Result

    Variable2 gets incorrect value.

    Expected Result

    Variable2 should be equal to 0.2

    Affected Browsers

    Tested on Chrome, Firefox, and Node-Webkit

    Operating System and Service Pack

    Win7

    Construct 2 Version ID

    I'm currently using r183, but I know that the bug was also in previous versions.

  • https://www.scirra.com/blog/141/common- ... nd-gotchas

    scroll a little past halfway down to "Expecting math calculations to be exact"

    or google floating point numbers and computers...

    (I once posted this as a bug, too. heh..)

  • Hey spacedoubt,

    Thanks for the comment.

    I know about the floating point issue, but I do not think it is valid here.

    After all, if I do: Set Variable2 to 1.2 - 1, Variable 2 will equal 0.2.

    And the example I gave is not so different.

  • 1.2+var1=2.2

    but....

    1.2-var1=0.199999999999996

    Strange indeed, no problem on addition. I understand if this is involve multiplication/division problem, but substraction?

  • It is rounding error. Just don't expect exact results with real (decimal) numbers.

    Try adding 0.1 to variable2 twelve times instead of 1.2 one time. You will get 2.200000000000001

    But it will display in a text field as 2.2

  • I wonder if this is a normal thing that everybody should expect?

  • Strange indeed, no problem on addition. I understand if this is involve multiplication/division problem, but substraction?

    That's what I thought.

    It is rounding error. Just don't expect exact results with real (decimal) numbers.

    Try adding 0.1 to variable2 twelve times instead of 1.2 one time. You will get 2.200000000000001

    But it will display in a text field as 2.2

    I am building a software that displays advanced graphs for external sensors system, the entire software is one big math problem.

    Of course, once I start division, multiplication, and other mathematical manipulations, I am aware of the floating point Inaccuracy.

    In case of 1.2 - 1 however, I do not expect it.

  • I am building a software that displays advanced graphs for external sensors system, the entire software is one big math problem.

    In C2 ? Seriously though, which language are you using for that? I just tried the 1.2 + 1.0 in C++ with similar results.

  • In C2 ? Seriously though, which language are you using for that? I just tried the 1.2 + 1.0 in C++ with similar results.

    Yes, in C2, it looks something like this:

    I use a Python server to connect to the sensors and MatLab for the functions, but all of the graphics calculations are done within the C2.

  • > In C2 ? Seriously though, which language are you using for that? I just tried the 1.2 + 1.0 in C++ with similar results.

    >

    Yes, in C2, it looks something like this:

    I use a Python server to connect to the sensors and MatLab for the functions, but all of the graphics calculations are done within the C2.

    ok man that's cool! sorry for the smiley face It looks great.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • ok man that's cool! sorry for the smiley face It looks great.

    Thanks

  • You can get around that (sort of) ...

    In Python I can write Round(Variable2, 4), when 4 represents the amount of decimal places which the software needs to round.

    It is equal to (Variable2 * 10000) / 10000

    Does anyone know if there is anything like that in C2?

  • Closing, spacedoubt is correct. This rounding happens in the floating point representation at the CPU level and it basically affects all software universally.

  • You can get around that (sort of) ...

    In Python I can write Round(Variable2, 4), when 4 represents the amount of decimal places which the software needs to round.

    It is equal to (Variable2 * 10000) / 10000

    Does anyone know if there is anything like that in C2?

    I think int(variable2*10000)/10000

    Also rounding errors can happen for storing decimals by themselves (some decimal numbers have not a finite number of decimals in binary), also, a negative number like -0.2 is not represented the same way as 0.2, which involves calculations and potential errors too.

  • It is still very accurate though. The error is at the 17th decimal place. The only inconvenience is making the number look cleaner when displaying it, which Aphrodite's equation will do.

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