#12489 Improve robustness of mean calculators

Replace asserts with early-return guards so zero or negative weights
are silently skipped instead of crashing. Add unit tests for zero-weight
and zero-value edge cases in all three calculator classes.

Fixes #12489.
This commit is contained in:
Kristian Bendiksen committed 2026-03-31 08:46:20 +02:00
1 parent ba1055fbc4
commit 394bc6c3df
6 files changed
+122 -13

No files matched your search

@@ -18,8 +18,6 @@
#include "RiaWeightedGeometricMeanCalculator.h"
#include "cvfAssert.h"
#include <cmath>
//--------------------------------------------------------------------------------------------------
@@ -36,7 +34,7 @@ RiaWeightedGeometricMeanCalculator::RiaWeightedGeometricMeanCalculator()
//--------------------------------------------------------------------------------------------------
void RiaWeightedGeometricMeanCalculator::addValueAndWeight( double value, double weight )
{
CVF_ASSERT( weight >= 0.0 );
if ( weight <= 0.0 ) return;
// This can be a very big number, consider other algorithms if that becomes a problem
m_aggregatedWeightedValue += ( std::log( value ) * weight );
@@ -18,8 +18,6 @@
#include "RiaWeightedHarmonicMeanCalculator.h"
#include "cvfAssert.h"
#include <cmath>
//--------------------------------------------------------------------------------------------------
@@ -36,7 +34,7 @@ RiaWeightedHarmonicMeanCalculator::RiaWeightedHarmonicMeanCalculator()
//--------------------------------------------------------------------------------------------------
void RiaWeightedHarmonicMeanCalculator::addValueAndWeight( double value, double weight )
{
CVF_ASSERT( weight > 1.0e-12 && std::abs( value ) > 1.0e-12 );
if ( weight <= 0.0 || std::abs( value ) < 1.0e-12 ) return;
m_aggregatedWeightedValue += weight / value;
m_aggregatedWeight += weight;
@@ -51,7 +49,6 @@ double RiaWeightedHarmonicMeanCalculator::weightedMean() const
{
return m_aggregatedWeight / m_aggregatedWeightedValue;
}
CVF_ASSERT( false );
return 0.0;
}
@@ -16,8 +16,6 @@
//
/////////////////////////////////////////////////////////////////////////////////
#include "cvfAssert.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
@@ -34,7 +32,7 @@ RiaWeightedMeanCalculator<T>::RiaWeightedMeanCalculator()
template <class T>
void RiaWeightedMeanCalculator<T>::addValueAndWeight( T value, double weight )
{
CVF_ASSERT( weight >= 0.0 );
if ( weight <= 0.0 ) return;
m_aggregatedValue = m_aggregatedValue + value * weight;
m_aggregatedWeight += weight;
@@ -46,9 +44,7 @@ void RiaWeightedMeanCalculator<T>::addValueAndWeight( T value, double weight )
template <class T>
T RiaWeightedMeanCalculator<T>::weightedMean() const
{
bool validWeights = validAggregatedWeight();
CVF_TIGHT_ASSERT( validWeights );
if ( validWeights )
if ( validAggregatedWeight() )
{
return m_aggregatedValue * ( 1.0 / m_aggregatedWeight );
}