Skip to content

fix: round float params to 6 significant digits on file save#82

Merged
rubenCodeforges merged 1 commit intorubenCodeforges:masterfrom
evanofficial:fix/float-precision-param-save
Mar 30, 2026
Merged

fix: round float params to 6 significant digits on file save#82
rubenCodeforges merged 1 commit intorubenCodeforges:masterfrom
evanofficial:fix/float-precision-param-save

Conversation

@evanofficial
Copy link
Copy Markdown
Contributor

Fixes #78.

Root cause

MAVLink transmits parameters as 32-bit single-precision floats. When the Electron main process receives them and stores them as JavaScript number (64-bit double), the conversion introduces floating-point noise:

ATC_RAT_PIT_P,0.18000000715255737   ← ArduDeck (before)
ATC_RAT_PIT_P,0.18                  ← MissionPlanner / mavproxy

The bug is on this line in PARAM_SAVE_FILE:

params.map(p => `${p.id},${p.value}`)

p.value is the raw double with noise.

Fix

Add a formatValue helper that applies toPrecision(6) (6 significant digits — the reliable precision of a 32-bit float) then parseFloat to strip trailing zeros:

const formatValue = (value: number): string => {
  if (Number.isInteger(value)) return String(value);
  return String(parseFloat(value.toPrecision(6)));
};

Before → After examples:

Raw value Before After
0.18000000715255737 0.18000000715255737 0.18
0.8999999761581421 0.8999999761581421 0.9
0.004000000189989805 0.004000000189989805 0.004
0.5 0.5 0.5
0 0 0

Test plan

  • Save parameters to file and confirm values match MissionPlanner output
  • Load the saved file back — values should round-trip correctly
  • npx tsc --noEmit passes
  • npx eslint . passes

I have read and agree to the ArduDeck Contributor License Agreement (CLA.md).

MAVLink sends 32-bit floats; decoding them into JS 64-bit doubles
introduces noise (e.g. 0.18000000715255737 instead of 0.18).
Applying toPrecision(6) + parseFloat matches MissionPlanner behaviour
and produces clean, human-readable parameter files.
@rubenCodeforges
Copy link
Copy Markdown
Owner

rubenCodeforges commented Mar 29, 2026

Im gonna get drunk to celebrate the first community PR !
@evanofficial Thank you very much , i already mentioned that we are currently on code freeze because ALPHA30 is in testing.

And ill check your pr tomorrow when im back at the HQ.

@evanofficial
Copy link
Copy Markdown
Contributor Author

evanofficial commented Mar 29, 2026

Hey @rubenCodeforges , thanks for the warm welcome! Honored to be the first community PR . No worries on the timeline, totally understand — take your time with the review. I'll be here if you need any changes or have feedback. Looking forward to it!

@rubenCodeforges
Copy link
Copy Markdown
Owner

Looks good to me , we will probably squeeze it in the Alpha 30 , @evanofficial great job , thank you !

@rubenCodeforges rubenCodeforges merged commit 486d74b into rubenCodeforges:master Mar 30, 2026
3 checks passed
@evanofficial evanofficial deleted the fix/float-precision-param-save branch March 30, 2026 17:59
@evanofficial
Copy link
Copy Markdown
Contributor Author

Looks good to me , we will probably squeeze it in the Alpha 30 , @evanofficial great job , thank you !

Thanks! glad to help out, excited to see it land in alpha 30

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ugly floats on parameters save

2 participants