Skip to content

Commit

Permalink
operator== should use tolerance by default
Browse files Browse the repository at this point in the history
Refs #11179
  • Loading branch information
DanNixon committed Feb 27, 2015
1 parent 180b6e7 commit 916e91c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
22 changes: 5 additions & 17 deletions Code/Mantid/Framework/API/src/NumericAxis.cpp
Expand Up @@ -146,14 +146,7 @@ size_t NumericAxis::indexOfValue(const double value) const {
* @return true if self and second axis are equal
*/
bool NumericAxis::operator==(const Axis &axis2) const {
if (length() != axis2.length()) {
return false;
}
const NumericAxis *spec2 = dynamic_cast<const NumericAxis *>(&axis2);
if (!spec2) {
return false;
}
return std::equal(m_values.begin(), m_values.end(), spec2->m_values.begin());
return equalWithinTolerance(axis2, 1e-15);
}

/** Check if two numeric axis are equivalent to a given tolerance
Expand All @@ -163,21 +156,16 @@ bool NumericAxis::operator==(const Axis &axis2) const {
*/
bool NumericAxis::equalWithinTolerance(const Axis &axis2,
const double tolerance) const {
const NumericAxis *spec2 = dynamic_cast<const NumericAxis *>(&axis2);
if (!spec2) {
if (length() != axis2.length()) {
return false;
}

const std::vector<double> otherValues = spec2->getValues();

// Fail comparison if number of values differs
if (m_values.size() != otherValues.size()) {
const NumericAxis *spec2 = dynamic_cast<const NumericAxis *>(&axis2);
if (!spec2) {
return false;
}

// Check each value is within tolerance
g_tolerance = tolerance;
return std::equal(m_values.begin(), m_values.end(), otherValues.begin(),
return std::equal(m_values.begin(), m_values.end(), spec2->m_values.begin(),
withinTolerance);
}

Expand Down
17 changes: 17 additions & 0 deletions Code/Mantid/Framework/API/test/NumericAxisTest.h
Expand Up @@ -172,6 +172,23 @@ class NumericAxisTest : public CxxTest::TestSuite
TS_ASSERT_EQUALS(4, axis.indexOfValue(5.4));
}

/**
* Default equality is tested to a tolerance of 1e-15.
*/
void test_equal()
{
double points1[] = {1.0, 2.0, 10e-16, 4.0, 5.0};
double points2[] = {1.0, 2.0, 20e-16, 4.0, 5.0}; // Just inside the tolerance
double points3[] = {1.0, 2.0, 21e-16, 4.0, 5.0}; // Just outsie the tolerance
const size_t npoints(5);
NumericAxis axis1(std::vector<double>(points1, points1 + npoints));
NumericAxis axis2(std::vector<double>(points2, points2 + npoints));
NumericAxis axis3(std::vector<double>(points3, points3 + npoints));

TS_ASSERT( axis1 == axis2 );
TS_ASSERT( !(axis1 == axis3) );
}

void test_equalWithinTolerance()
{
double points1[] = {1.0, 2.0, 3.0, 4.0, 5.0};
Expand Down

0 comments on commit 916e91c

Please sign in to comment.