Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Fuzzy_equal_to

 

./CppFuzzy_equal_to/CppFuzzy_equal_to.pri

 

INCLUDEPATH += \
    ../../Classes/CppFuzzy_equal_to

SOURCES +=
#    ../../Classes/CppFuzzy_equal_to/fuzzy_equal_to.cpp

HEADERS  += \
    ../../Classes/CppFuzzy_equal_to/fuzzy_equal_to.h

OTHER_FILES += \
    ../../Classes/CppFuzzy_equal_to/Licence.txt

 

 

 

 

 

./CppFuzzy_equal_to/fuzzy_equal_to.h

 

//---------------------------------------------------------------------------
/*
fuzzy_equal_to, fuzzy equal_to predicate
Copyright (C) 2011-2015 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppFuzzy_equal_to.htm
//---------------------------------------------------------------------------
#ifndef RIBI_FUZZY_EQUAL_TO_H
#define RIBI_FUZZY_EQUAL_TO_H

#include <cassert>
#include <cmath>
#include <functional>
#include <limits>
#include <string>
#include <vector>


namespace ribi {

///fuzzy_equal_to is a predicate to test two doubles for equality
///with a certain tolerance. A tolerance of 0.0 denotes that
///an exact match is requested. Note that the value of 0.0 cannot
///be compared fuzzily.
///From http://www.richelbilderbeek.nl/CppFuzzy_equal_to.htm
struct fuzzy_equal_to
{
  explicit fuzzy_equal_to(const double tolerance = std::numeric_limits<double>::epsilon())
    : m_tolerance(tolerance)
  {
    assert(tolerance >= 0.0);
  }
  bool operator()(const double lhs, const double rhs) const
  {
    assert(lhs != 0.0);
    return rhs > (1.0 - m_tolerance) * lhs
        && rhs < (1.0 + m_tolerance) * lhs;
  }
  const double m_tolerance;

  static std::string GetVersion()
  {
    return "1.4";
  }
  static std::vector<std::string> GetVersionHistory()
  {
    return {
      "2011-02-24: version 1.0: initial version",
      "2012-11-14: version 1.1: set default tolerance to std::numeric_limits<double>::epsilon()",
      "2012-11-18: version 1.2: added #pragma's to file to suppress -Weffc++ warnings",
      "2012-11-18: version 1.3: added #include <limits>",
      "2014-12-27: version 1.4: do not inherit from deprecated std::binary_function"
    };
  }
};

///fuzzy_equal_to_abs is a predicate to test two doubles for equality
///by allowing a certain absolute difference between the two values
///A tolerance of 0.0 denotes that an exact match is requested.
///From http://www.richelbilderbeek.nl/CppFuzzy_equal_to.htm
struct fuzzy_equal_to_abs
{
  explicit fuzzy_equal_to_abs(const double tolerance = std::numeric_limits<double>::epsilon())
    : m_tolerance(tolerance)
  {
    assert(tolerance >= 0.0);
  }
  bool operator()(const double lhs, const double rhs) const noexcept
  {
    return std::abs(lhs-rhs) < m_tolerance;
  }
  const double m_tolerance;

  static std::string GetVersion() noexcept
  {
    return "1.0";
  }
  static std::vector<std::string> GetVersionHistory() noexcept
  {
    return {
      "2013-11-05: version 1.0: initial version from fuzzy_equal_to"
    };
  }
};

} //~namespace ribi

#endif // RIBI_FUZZY_EQUAL_TO_H

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml