Skip to content
Snippets Groups Projects
Commit ba2dff7f authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

Change math of areAlmostEqual: rm non-monotonic step from dependence on |b|.

parent dca1bf89
No related branches found
No related tags found
No related merge requests found
...@@ -14,16 +14,20 @@ ...@@ -14,16 +14,20 @@
// ************************************************************************** // // ************************************************************************** //
#include "Numeric.h" #include "Numeric.h"
#include <algorithm>
#include <cmath> #include <cmath>
#include <limits>
//! Floating-point epsilon, tolerances, almost-equal. //! Floating-point epsilon, tolerances, almost-equal.
namespace Numeric { namespace Numeric {
//! compare two doubles //! Returns true if two doubles agree within epsilon*tolerance
bool areAlmostEqual(double a, double b, double tolerance_factor) bool areAlmostEqual(double a, double b, double tolerance)
{ {
return get_relative_difference(a, b) < tolerance_factor*double_epsilon; constexpr double eps = std::numeric_limits<double>::epsilon();
return std::abs(a-b) <= eps * std::max( tolerance*eps, std::max(1., tolerance)*std::abs(b) );
// return get_relative_difference(a, b) < tolerance_factor*double_epsilon;
} }
//! calculates safe relative difference |(a-b)/b| //! calculates safe relative difference |(a-b)/b|
......
...@@ -171,8 +171,7 @@ bool FitSuiteParameters::numbersDiffer(double a, double b, double tol) const ...@@ -171,8 +171,7 @@ bool FitSuiteParameters::numbersDiffer(double a, double b, double tol) const
constexpr double eps = std::numeric_limits<double>::epsilon(); constexpr double eps = std::numeric_limits<double>::epsilon();
if (tol<1) if (tol<1)
throw std::runtime_error("Bug: FitSuiteParameters::numbersDiffer not intended for tol<1"); throw std::runtime_error("Bug: FitSuiteParameters::numbersDiffer not intended for tol<1");
return ( std::abs(b)<=eps && std::abs(a-b) > tol * eps * eps ) || return std::abs(a-b) > eps * std::max( tol*eps, std::abs(b) );
std::abs(a-b) > eps * std::abs(b);
} }
void FitSuiteParameters::printFitParameters() const void FitSuiteParameters::printFitParameters() const
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment