Skip to content

Commit 787f1ab

Browse files
authored
Merge pull request InsightSoftwareConsortium#5909 from hjmjohnson/fix-cumulative-gaussian-divide-by-zero
BUG: Fix divide-by-zero and integer truncation in CalculateFitError
2 parents c79274e + 1d58342 commit 787f1ab

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)