diff --git a/ChangeLog b/ChangeLog index 14abaa0..e2a1930 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,7 @@ * Version 2.0.0 released ======================== + Add gammalib::compare() function (#3552) Add DRI constructor to GCOMObservation class. Add member access methods to GCOMDri class. Implement ToF correction for COMPTEL EVP version 2 datasets (#3549) diff --git a/NEWS b/NEWS index 3825b5d..9bebc61 100644 --- a/NEWS +++ b/NEWS @@ -80,6 +80,7 @@ The following methods have been added: - GCOMDri::num_used_superpackets() - GCOMDri::num_skipped_superpackets() - gammalib::tmpnam() +- gammalib::compare() The following methods have been removed: - none @@ -209,6 +210,9 @@ Added GSkyRegionRect class to implement a rectangular region on the sky (#3248). 13. Support module ------------------ +Added gammalib::compare() function to compare two floating point numbers with a +relative tolerance (#3552). + Added gammalib::tmpnam() function to return a temporary file name, in replacement of the deprecated str::tmpnam() function (#3324). diff --git a/doc/source/admin/release_history/2.0.rst b/doc/source/admin/release_history/2.0.rst index e8ff2b2..a91f887 100644 --- a/doc/source/admin/release_history/2.0.rst +++ b/doc/source/admin/release_history/2.0.rst @@ -29,6 +29,8 @@ Bug fixes Improvements ------------ +* [`3552 `_] - + Add ``gammalib::compare()`` function * Add DRI constructor to ``GCOMObservation`` class * Add member access methods to ``GCOMDri`` class * [`3549 `_] - diff --git a/include/GTools.hpp b/include/GTools.hpp index 95a26b9..9ae4392 100644 --- a/include/GTools.hpp +++ b/include/GTools.hpp @@ -1,7 +1,7 @@ /*************************************************************************** * GTools.hpp - GammaLib tools * * ----------------------------------------------------------------------- * - * copyright (C) 2008-2020 by Juergen Knoedlseder * + * copyright (C) 2008-2021 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -151,6 +151,9 @@ namespace gammalib { const double& sindist, const double& roi, const double& cosroi); + bool compare(const double& a, + const double& b, + const double& tol); } diff --git a/pyext/GTools.i b/pyext/GTools.i index 9ee110b..38a1dfe 100644 --- a/pyext/GTools.i +++ b/pyext/GTools.i @@ -1,7 +1,7 @@ /*************************************************************************** * GTools.i - GammaLib tools * * ----------------------------------------------------------------------- * - * copyright (C) 2008-2020 by Juergen Knoedlseder * + * copyright (C) 2008-2021 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -106,4 +106,7 @@ namespace gammalib { const double& sindist, const double& roi, const double& cosroi); + bool compare(const double& a, + const double& b, + const double& tol); } diff --git a/src/support/GTools.cpp b/src/support/GTools.cpp index c169d90..9478d22 100644 --- a/src/support/GTools.cpp +++ b/src/support/GTools.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * GTools.cpp - GammaLib tools * * ----------------------------------------------------------------------- * - * copyright (C) 2008-2020 by Juergen Knoedlseder * + * copyright (C) 2008-2021 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -1979,3 +1979,49 @@ double gammalib::roi_arclength(const double& rad, const double& dist, // Return arclength return arclength; } + + +/***********************************************************************//** + * @brief Compare two floating point values with tolerance + * + * @param[in] a First floating point value. + * @param[in] b Second floating point value. + * @param[in] tol Relative tolerance. + * @return True if both floating point values are identical within + * the tolerance. + * + * This method tests whether @p a is identical to @p b within a given + * relative tolerance @p tol. The test verifies whether + * + * \f[|a - b| \le |a \times tol|\f] + * + * for \f$a \ne 0\f$ or whether + * + * \f[|a - b| \le |b \times tol|\f] + * + * for \f$b \ne 0\f$. If both \f$a = 0\f$ and \f$b = 0\f$ the method + * returns true. + ***************************************************************************/ +bool gammalib::compare(const double& a, const double& b, const double& tol) +{ + // Initialise identity + bool identity = false; + + // Case A: a is not zero + if (a != 0.0) { + identity = std::abs(a - b) <= std::abs(a * tol); + } + + // Case B: b is not zero + else if (b != 0.0) { + identity = std::abs(a - b) <= std::abs(b * tol); + } + + // Case C: a and b are zero + else { + identity = true; + } + + // Return identity + return identity; +} diff --git a/test/test_GSupport.cpp b/test/test_GSupport.cpp index 6e2cf12..ac46b4c 100644 --- a/test/test_GSupport.cpp +++ b/test/test_GSupport.cpp @@ -1,7 +1,7 @@ /*************************************************************************** * test_GSupport.cpp - test support module * * ----------------------------------------------------------------------- * - * copyright (C) 2010-2020 by Juergen Knoedlseder * + * copyright (C) 2010-2021 by Juergen Knoedlseder * * ----------------------------------------------------------------------- * * * * This program is free software: you can redistribute it and/or modify * @@ -147,6 +147,15 @@ void TestGSupport::test_tools(void) test_assert(!gammalib::contains(strings, "b"), "\"b\" not in vector"); test_assert(!gammalib::contains(strings, "isavector"), "\"isavector\" not in vector"); + // Test compare method + test_assert(gammalib::compare(261.0, 260.999999, 1.0e-8), "Compare positive floats for tol=1e-8"); + test_assert(!gammalib::compare(261.0, 260.999999, 1.0e-9), "Compare positive floats for tol=1e-9"); + test_assert(gammalib::compare(-261.0, -260.999999, 1.0e-8), "Compare negative floats for tol=1e-8"); + test_assert(!gammalib::compare(-261.0, -260.999999, 1.0e-9), "Compare negative floats for tol=1e-9"); + test_assert(gammalib::compare(0.0, 0.0000001, 1.0), "Compare near zero floats for tol=1.0"); + test_assert(!gammalib::compare(0.0, 0.0000001, 0.1), "Compare near zero floats for tol=0.1"); + test_assert(gammalib::compare(0.0, 0.0, 1.0e-10), "Compare zero floats for tol=1e-10"); + // Return return; }