From 1c1190254d9d1beced40cc915c7967d5de988fed Mon Sep 17 00:00:00 2001 From: Bob Long Date: Sun, 11 Aug 2024 14:31:24 +1000 Subject: [PATCH] MAVLinkParam: fix rounding to 7 digits Rounding to fixed number of decimal places led to over rounding small numbers and under-rounding big numbers. --- ExtLibs/Mavlink/MAVLinkParam.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ExtLibs/Mavlink/MAVLinkParam.cs b/ExtLibs/Mavlink/MAVLinkParam.cs index 70402aee09..e5d8c1e568 100644 --- a/ExtLibs/Mavlink/MAVLinkParam.cs +++ b/ExtLibs/Mavlink/MAVLinkParam.cs @@ -129,12 +129,21 @@ public double GetValue() (double)item.float_value 0.800000011920929 */ - return Math.Round((double)float_value, 7); + return RoundToSignificantDigits(float_value, 7); } throw new FormatException("invalid type"); } + private double RoundToSignificantDigits(double d, int digits) + { + if (d == 0) + return 0; + + double scale = Math.Pow(10, Math.Floor(Math.Log10(Math.Abs(d))) + 1); + return scale * Math.Round(d / scale, digits); + } + public void SetValue(double input) { switch (Type)