bitfocus / companion

Bitfocus Companion enables the reasonably priced Elgato Stream Deck and other controllers to be a professional shotbox surface for an increasing amount of different presentation switchers, video playback software and broadcast equipment.
http://bitfocus.io/companion
Other
1.62k stars 505 forks source link

[BUG] v3.4.1 Stable - Internal Variable Rounding Error #3066

Open Poncho42 opened 1 month ago

Poncho42 commented 1 month ago

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

  1. Create internal Variable $(internal:custon_round)
  2. Set Start value to 0.5
  3. Make a Button
  4. Add Action "internal: Custom Variable: Set with expression"
  5. Change Custom Variable to "A custom Variable (internal:custon_round)
  6. input "$(internal:custom_round)+0.1
  7. Press Button 2-5 times and see the rounding issue

Expected Behavior

After 3 presses expected output is 0.8, actual output is 0.7999999999999999

Environment (please complete the following information)

- OS: Windows 11 Pro
- Browser: Chrome
- Companion Version: v3.4.1

Additional context

First couple of button presses add properly, but the farther you get from the starting value the more off it gets.

thedist commented 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).

Poncho42 commented 1 month ago

interesting. is there something similar i can put into the formula bar similar to excel's "round(variable, 0)"?

thedist commented 1 month ago

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.