[Liblas-commits] libpc: add FP compare support

liblas-commits at liblas.org liblas-commits at liblas.org
Wed Feb 23 20:59:24 EST 2011


details:   http://hg.liblas.orglibpc/rev/13177ebbdcd8
changeset: 82:13177ebbdcd8
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Wed Feb 23 17:59:11 2011 -0800
description:
add FP compare support

diffstat:

 include/libpc/Utils.hpp |  10 +++++++++-
 test/unit/UtilsTest.cpp |  30 +++++++++++++++++++++++-------
 2 files changed, 32 insertions(+), 8 deletions(-)

diffs (71 lines):

diff -r 5b9465f9d5ad -r 13177ebbdcd8 include/libpc/Utils.hpp
--- a/include/libpc/Utils.hpp	Wed Feb 23 17:22:36 2011 -0800
+++ b/include/libpc/Utils.hpp	Wed Feb 23 17:59:11 2011 -0800
@@ -52,13 +52,21 @@
     static void Utils::random_seed(unsigned int seed);
     static double random(double minimum, double maximum);
 
+    // compares two values to within the datatype's epsilon
     template<class T>
     static bool compare_distance(const T& actual, const T& expected)
     {
         const T epsilon = std::numeric_limits<T>::epsilon();
+        return compare_approx<T>(actual, expected, epsilon);
+    }
+
+    // compares two values to within a given tolerance
+    template<class T>
+    static bool compare_approx(const T& actual, const T& expected, const T& tolerance)
+    {
         const T diff = actual - expected;
 
-        if ( !((diff <= epsilon) && (diff >= -epsilon )) )
+        if ( !((diff <= tolerance) && (diff >= -tolerance )) )
         {
             return false;
         }
diff -r 5b9465f9d5ad -r 13177ebbdcd8 test/unit/UtilsTest.cpp
--- a/test/unit/UtilsTest.cpp	Wed Feb 23 17:22:36 2011 -0800
+++ b/test/unit/UtilsTest.cpp	Wed Feb 23 17:59:11 2011 -0800
@@ -38,18 +38,34 @@
 }
 
 
-BOOST_AUTO_TEST_CASE(test_eps)
+BOOST_AUTO_TEST_CASE(test_comparators)
 {
     bool ok;
     
-    ok = Utils::compare_distance<float>(1.000001f, 1.0f);
-    BOOST_CHECK(!ok);
+    {
+        ok = Utils::compare_distance<float>(1.000001f, 1.0f);
+        BOOST_CHECK(!ok);
 
-    ok = Utils::compare_distance<float>(1.0000001f, 1.0f);
-    BOOST_CHECK(ok);
+        ok = Utils::compare_distance<float>(1.0000001f, 1.0f);
+        BOOST_CHECK(ok);
 
-    ok = Utils::compare_distance<float>(1.00000001f, 1.0f);
-    BOOST_CHECK(ok);
+        ok = Utils::compare_distance<float>(1.00000001f, 1.0f);
+        BOOST_CHECK(ok);
+    }
+
+    {
+        ok = Utils::compare_approx<float>(1.001f, 1.0f, 0.0001);
+        BOOST_CHECK(!ok);
+
+        ok = Utils::compare_approx<float>(1.001f, 1.0f, 0.001);
+        BOOST_CHECK(!ok);
+
+        ok = Utils::compare_approx<float>(1.001f, 1.0f, 0.01);
+        BOOST_CHECK(ok);
+
+        ok = Utils::compare_approx<float>(1.001f, 1.0f, 0.1);
+        BOOST_CHECK(ok);
+    }
 }
 
 


More information about the Liblas-commits mailing list