Skip to content

Commit

Permalink
Correct NaN and infinity comparison
Browse files Browse the repository at this point in the history
Refs #11179
  • Loading branch information
DanNixon committed Feb 27, 2015
1 parent 65a6bac commit 3573429
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion Code/Mantid/Framework/API/src/NumericAxis.cpp
Expand Up @@ -6,6 +6,7 @@
#include "MantidKernel/VectorHelper.h"

#include <boost/format.hpp>
#include <boost/math/special_functions/fpclassify.hpp>

#include "MantidKernel/Logger.h"
namespace {
Expand All @@ -14,7 +15,13 @@ Mantid::Kernel::Logger g_log("NumericAxis");
class EqualWithinTolerance {
public:
EqualWithinTolerance(double tolerance) : m_tolerance(tolerance) {};
bool operator()(double a, double b) { return std::abs(a - b) <= m_tolerance; }
bool operator()(double a, double b) {
if (boost::math::isnan(a) && boost::math::isnan(b))
return true;
if (boost::math::isinf(a) && boost::math::isinf(b))
return true;
return std::abs(a - b) <= m_tolerance;
}

private:
double m_tolerance;
Expand Down
8 changes: 4 additions & 4 deletions Code/Mantid/Framework/API/test/NumericAxisTest.h
Expand Up @@ -206,8 +206,8 @@ class NumericAxisTest : public CxxTest::TestSuite

void test_equalWithinTolerance_Nan()
{
double points1[] = {1.0, 2.0, FP_NAN, 4.0, 5.0};
double points2[] = {1.0, 2.0, FP_NAN, 4.0, 5.0};
double points1[] = {1.0, 2.0, NAN, 4.0, 5.0};
double points2[] = {1.0, 2.0, NAN, 4.0, 5.0};
double points3[] = {1.0, 2.0, 3.0, 4.0, 5.0};
const size_t npoints(5);
NumericAxis axis1(std::vector<double>(points1, points1 + npoints));
Expand All @@ -220,8 +220,8 @@ class NumericAxisTest : public CxxTest::TestSuite

void test_equalWithinTolerance_Inf()
{
double points1[] = {1.0, 2.0, FP_INFINITE, 4.0, 5.0};
double points2[] = {1.0, 2.0, FP_INFINITE, 4.0, 5.0};
double points1[] = {1.0, 2.0, INFINITY, 4.0, 5.0};
double points2[] = {1.0, 2.0, INFINITY, 4.0, 5.0};
double points3[] = {1.0, 2.0, 3.0, 4.0, 5.0};
const size_t npoints(5);
NumericAxis axis1(std::vector<double>(points1, points1 + npoints));
Expand Down

0 comments on commit 3573429

Please sign in to comment.