Open Poncho42 opened 1 month ago
Javascript is notoriously bad for floating point math. I'm not sure if this is something that can easily be fixed on Companion's side, or more so raise awareness on the users side so that appropriate steps can be taken to handle the math inaccuracies of JS as different users may accept different ways to handle it (including not handling it at all if the issue is insignificant for their use case).
interesting. is there something similar i can put into the formula bar similar to excel's "round(variable, 0)"?
If you press the Getting Started button in the Companion web ui, and then go to Secondary admin controls > Expressions > Functions, it'll show how to do rounding, ceiling, floor, things like that within Expressions.
Another way to handle JS's math issues is to deal with whole numbers when manipulating them, then dividing them when you want to use/display them. For example rather than using 0.5, and then adding/subtracting 0.1, instead start with 5 and add/subtract 1, and if you want to display that value on a button you could do the Expression $(internal:custom_round) / 10
, which should be accurate. If you wanted to use it as an option in an action or feedback you could use a trigger so that when $(internal:custom_round)
changes, $(internal:custom_round_floating_point)
or something changes which would be $(internal:custom_round) / 10
. None of these are particularly ideal, and it all adds extra steps in, but it's one of the quirks of JS unfortunately.
Is this a bug in companion itself or a module?
Is there an existing issue for this?
Describe the bug
Incrementing a custom internal variable by 0.1 has rounding errors making numbers 18 significant digits long
Steps To Reproduce
Expected Behavior
After 3 presses expected output is 0.8, actual output is 0.7999999999999999
Environment (please complete the following information)
Additional context
First couple of button presses add properly, but the farther you get from the starting value the more off it gets.