Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Updated to use Richard Harris' approximation formula

Commit
d7b8b01f3e6891a3e71b806011b01562a5f27a89
Author
Phil Nash ·
Parents
d7c203c3e9c3

Files · Download .tar.gz

Changes

 include/internal/catch_approx.hpp | 40 ++++++++++++++++-----------------------
 1 file changed, 16 insertions(+), 24 deletions(-)

diff --git a/include/internal/catch_approx.hpp b/include/internal/catch_approx.hpp
index 538441a2..c77f8786 100644
--- a/include/internal/catch_approx.hpp
+++ b/include/internal/catch_approx.hpp
@@ -18,27 +18,19 @@
 
 namespace Catch
 {
-    
-    // !TBD Need to clean this all up
-#define CATCH_absTol 1e-10
-#define CATCH_relTol 1e-10
-    
-    inline double catch_max( double x, double y )
-    {
-        return x > y ? x : y;
-    }
     namespace Detail
     {
         class Approx
         {
         public:
             ///////////////////////////////////////////////////////////////////////////
-            // !TBD more generic
             explicit Approx
             (
-             double d
-             )
-            : m_d( d )
+                double d
+            )
+            :   m_epsilon( 1e-10 ),
+                m_scale( 1.0 ),
+                m_d( d )
             {
             }
             
@@ -46,27 +38,27 @@ namespace Catch
             template<typename T>
             friend bool operator == 
             (
-             const T& lhs, 
-             const Approx& rhs
-             )
+                const T& lhs, 
+                const Approx& rhs
+            )
             {
-                // !TBD Use proper tolerance
-                // From: http://realtimecollisiondetection.net/blog/?p=89
-                // see also: http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
-                return fabs( lhs - rhs.m_d ) <= catch_max( CATCH_absTol, CATCH_relTol * catch_max( fabs(lhs), fabs(rhs.m_d) ) );
+                // Thanks to Richard Harris for his help refining this formula
+                return fabs( lhs - rhs.m_d ) < rhs.m_epsilon * (rhs.m_scale + (std::max)( fabs(lhs), fabs(rhs.m_d) ) );
             }
             
             ///////////////////////////////////////////////////////////////////////////
             template<typename T>
             friend bool operator != 
             (
-             const T& lhs, 
-             const Approx& rhs
-             )
+                const T& lhs, 
+                const Approx& rhs
+            )
             {
-                return ! operator==( lhs, rhs );
+                return !operator==( lhs, rhs );
             }
             
+            double m_epsilon;
+            double m_scale;
             double m_d;
         };
     }