Skip to content

CompareWorkspaces with relative tolerance incorrectly evaluating to true #37877

@rboston628

Description

@rboston628

Describe the bug

CompareWorkspaces is not properly handling relative differences when the size of the values is smaller than the tolerance.

This error is due to L1361-1362 in CompareWorkspaces.cpp. I am unsure why this was originally implemented this way.

The tests of CompareWorkspaces are under-developed for testing relative errors. Only a single test tries to make use of relative differences, while more should be used.

To Reproduce
Open workbench. Run the following:

# import mantid algorithms, numpy and matplotlib
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np

tol = 0.01
x1 = 0.00001
x2 = 0.00010
ws1 = CreateWorkspace(DataX=[x1], DataY=[1])
ws2 = CreateWorkspace(DataX=[x2], DataY=[1])
print( (x2-x1)/(0.5 * (x2+x1)) )
assert not (x2-x1)/(0.5 * (x2+x1)) <= tol
res, msg = CompareWorkspaces(ws1, ws2, Tolerance=tol, ToleranceRelErr=True)
print(res)
assert not res

The relative tolerance is > 1, and the relative tolerance is 0.01, and yet the last assert will fail (meaning the workspaces compare equal).

Expected behavior
Checks of relative error with CompareWorkspaces should fail if the relative error is above the tolerance. Suggested implementation

bool CompareWorkspaces::relErr(const double x1, const double x2, const double rtol) const {
  const double numerator = std::fabs(x1 - x2);
  if (numerator == 0.)
    return true;

  const double denomenator = 0.5 * std::fabs(x1 + x2);

  return numerator < rtol * denomenator;
}

To make this easier to add unit tests, making this method static and public would be helpful.

Platform/Version (please complete the following information):

  • OS: Ubuntu
  • OS Version: 22.04.4
  • Mantid Version: 6.10.20240823

Additional context

Was discovered while writing tests in PR #37878
Will be fixed by PR #37889

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIssues and pull requests that are regressions or would be considered a bug by users (e.g. crashing)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions