diff --git a/src/lib/mathlib/math/WelfordMean.hpp b/src/lib/mathlib/math/WelfordMean.hpp index ac6af40faa73..80d6b68a6075 100644 --- a/src/lib/mathlib/math/WelfordMean.hpp +++ b/src/lib/mathlib/math/WelfordMean.hpp @@ -58,6 +58,9 @@ class WelfordMean // M2 aggregates the squared distance from the mean // count aggregates the number of samples seen so far _M2 += delta.emult(new_value - _mean); + + // protect against floating point precision causing negative variances + _M2 = matrix::max(_M2, {}); } bool valid() const { return _count > 2; }