From 2585d280d1e03ed5fdc09cdb4a58798713f6d26b Mon Sep 17 00:00:00 2001 From: cnugteren Date: Thu, 12 Nov 2015 15:07:20 +0100 Subject: [PATCH 1/2] Added an optional absolute margin to the approximation checks --- include/internal/catch_approx.hpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index f5dba61a..5dd7df8e 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -20,12 +20,14 @@ namespace Detail { public: explicit Approx ( double value ) : m_epsilon( std::numeric_limits::epsilon()*100 ), + m_margin( 0.0 ), m_scale( 1.0 ), m_value( value ) {} Approx( Approx const& other ) : m_epsilon( other.m_epsilon ), + m_margin( other.m_margin ), m_scale( other.m_scale ), m_value( other.m_value ) {} @@ -37,13 +39,19 @@ namespace Detail { Approx operator()( double value ) { Approx approx( value ); approx.epsilon( m_epsilon ); + approx.margin( m_margin ); approx.scale( m_scale ); return approx; } friend bool operator == ( double lhs, Approx const& rhs ) { // Thanks to Richard Harris for his help refining this formula - return fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) ); + auto relativeOK = fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) ); + if ( relativeOK ) { + return true; + } + auto absoluteOK = fabs( lhs - rhs.m_value ) < rhs.m_margin; + return absoluteOK; } friend bool operator == ( Approx const& lhs, double rhs ) { @@ -63,6 +71,11 @@ namespace Detail { return *this; } + Approx& margin( double newMargin ) { + m_margin = newMargin; + return *this; + } + Approx& scale( double newScale ) { m_scale = newScale; return *this; @@ -76,6 +89,7 @@ namespace Detail { private: double m_epsilon; + double m_margin; double m_scale; double m_value; }; From 3523c39f44c855c1d6f0d7ef3d3a3ea12d4a8987 Mon Sep 17 00:00:00 2001 From: CNugteren Date: Thu, 12 Nov 2015 15:31:42 +0100 Subject: [PATCH 2/2] Changed 'auto' into 'bool' for C++98 compatibility --- include/internal/catch_approx.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp index 5dd7df8e..08a15b71 100644 --- a/include/internal/catch_approx.hpp +++ b/include/internal/catch_approx.hpp @@ -46,11 +46,11 @@ namespace Detail { friend bool operator == ( double lhs, Approx const& rhs ) { // Thanks to Richard Harris for his help refining this formula - auto relativeOK = fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) ); + bool relativeOK = fabs( lhs - rhs.m_value ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_value) ) ); if ( relativeOK ) { return true; } - auto absoluteOK = fabs( lhs - rhs.m_value ) < rhs.m_margin; + bool absoluteOK = fabs( lhs - rhs.m_value ) < rhs.m_margin; return absoluteOK; }