Просмотр исходного кода

Fix checkDigitConsistency corrupting sub-digits of large readings

checkDigitConsistency takes a double, does all its arithmetic in double,
but was declared to return float. Once a meter reading exceeds ~1,000,000
(7+ significant digits) the whole-number part alone consumes 32-bit
float's ~7-digit precision budget, so the implicit double->float narrowing
at the return statement silently mangles the sub-digit decimals - with no
rounding rule involved, purely running out of mantissa bits. Observed on a
water meter at 2116691.67: the value was published as 2116691.75, a
0.08 L error appearing every round that the (enabled) digit-consistency
check runs.

Return double and use a double working variable throughout, matching the
double precision used everywhere else in the post-processing pipeline. The
digit-extraction intermediate (zw) is likewise widened to double so digit
values stay correct at large magnitudes.
David Schug 4 месяцев назад
Родитель
Сommit
d30e11f930

+ 6 - 0
Changelog.md

@@ -1,3 +1,9 @@
+# [Unreleased]
+
+### Core Changes and Bug fixes
+- Fixed `checkDigitConsistency` corrupting the sub-digit part of large readings: it computed in `double` but returned `float`, so once a meter passed ~1,000,000 (7+ significant digits) the narrowing to 32-bit float mangled the decimals (e.g. a reading of `…691.67` was published as `…691.75`). The function now returns `double` and uses a `double` working variable throughout.
+
+
 # [16.1.0] - 2026-01-11
 
 For a full list of changes see [Full list of changes](https://github.com/jomjol/AI-on-the-edge-device/compare/v16.0.0...v16.1.0)

+ 4 - 4
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

@@ -1106,11 +1106,11 @@ string ClassFlowPostProcessing::ErsetzteN(string input, double _prevalue) {
     return input;
 }
 
-float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
+double ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue) {
     int aktdigit, olddigit;
     int aktdigit_before, olddigit_before;
     int pot, pot_max;
-    float zw;
+    double zw;
     bool no_nulldurchgang = false;
 
     pot = _decilamshift;
@@ -1142,13 +1142,13 @@ float ClassFlowPostProcessing::checkDigitConsistency(double input, int _decilams
 
         if (no_nulldurchgang) {
             if (aktdigit != olddigit) {
-                input = input + ((float) (olddigit - aktdigit)) * pow(10, pot);     // New Digit is replaced by old Digit;
+                input = input + ((double) (olddigit - aktdigit)) * pow(10, pot);     // New Digit is replaced by old Digit;
             }
         }
         else {
             // despite zero crossing, digit was not incremented --> add 1
             if (aktdigit == olddigit) {
-                input = input + ((float) (1)) * pow(10, pot);   // add 1 at the point
+                input = input + ((double) (1)) * pow(10, pot);   // add 1 at the point
             }
         }
 			

+ 1 - 1
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.h

@@ -30,7 +30,7 @@ protected:
     string ShiftDecimal(string in, int _decShift);
 
     string ErsetzteN(string, double _prevalue);
-    float checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue);
+    double checkDigitConsistency(double input, int _decilamshift, bool _isanalog, double _preValue);
 
     void InitNUMBERS();