Skip to content

Commit 1d58342

Browse files
hjmjohnsonclaude
andcommitted
BUG: Fix divide-by-zero and integer truncation in CalculateFitError
Two bugs in CumulativeGaussianCostFunction::CalculateFitError(): 1. Division by zero: if m_OriginalDataArray has zero elements, dividing by numberOfElements is undefined behavior. Guard added to the existing size-mismatch early return. 2. Integer truncation: `(1 / numberOfElements) * fitError` performs integer division, yielding 0 for any numberOfElements > 1 and making the RMSE always return 0.0. The correct RMSE formula is sqrt(fitError / numberOfElements). Detected by clang-22 scan-build (core.DivideZero checker). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 9bfc81b commit 1d58342

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

Modules/Numerics/Optimizers/src/itkCumulativeGaussianCostFunction.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ CumulativeGaussianCostFunction::CalculateFitError(MeasureType * setTestArray)
4343
// Use root mean square error as a measure of fit quality.
4444
const unsigned int numberOfElements = m_OriginalDataArray.GetNumberOfElements();
4545

46-
if (numberOfElements != setTestArray->GetNumberOfElements())
46+
if (numberOfElements != setTestArray->GetNumberOfElements() || numberOfElements == 0)
4747
{
4848
return 1;
4949
}
@@ -52,7 +52,7 @@ CumulativeGaussianCostFunction::CalculateFitError(MeasureType * setTestArray)
5252
{
5353
fitError += Math::sqr(setTestArray->get(i) - m_OriginalDataArray.get(i));
5454
}
55-
return (std::sqrt((1 / numberOfElements) * fitError));
55+
return std::sqrt(fitError / static_cast<double>(numberOfElements));
5656
}
5757

5858
double

0 commit comments

Comments
 (0)