Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an optional absolute margin to the approximation checks #561

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion include/internal/catch_approx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ namespace Detail {
public:
explicit Approx ( double value )
: m_epsilon( std::numeric_limits<float>::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 )
{}
Expand All @@ -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) ) );
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;
}
bool absoluteOK = fabs( lhs - rhs.m_value ) < rhs.m_margin;
return absoluteOK;
}

friend bool operator == ( Approx const& lhs, double rhs ) {
Expand All @@ -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;
Expand All @@ -76,6 +89,7 @@ namespace Detail {

private:
double m_epsilon;
double m_margin;
double m_scale;
double m_value;
};
Expand Down