Commit 890fb52143dcf12f96b785e77e8adbdb8f267450
1 parent
6afd90f2
Add gammalib::compare() function (#3552)
Showing
7 changed files
with
72 additions
and
4 deletions
ChangeLog
| @@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
| 3 | * Version 2.0.0 released | 3 | * Version 2.0.0 released |
| 4 | ======================== | 4 | ======================== |
| 5 | 5 | ||
| 6 | + Add gammalib::compare() function (#3552) | ||
| 6 | Add DRI constructor to GCOMObservation class. | 7 | Add DRI constructor to GCOMObservation class. |
| 7 | Add member access methods to GCOMDri class. | 8 | Add member access methods to GCOMDri class. |
| 8 | Implement ToF correction for COMPTEL EVP version 2 datasets (#3549) | 9 | Implement ToF correction for COMPTEL EVP version 2 datasets (#3549) |
NEWS
| @@ -80,6 +80,7 @@ The following methods have been added: | @@ -80,6 +80,7 @@ The following methods have been added: | ||
| 80 | - GCOMDri::num_used_superpackets() | 80 | - GCOMDri::num_used_superpackets() |
| 81 | - GCOMDri::num_skipped_superpackets() | 81 | - GCOMDri::num_skipped_superpackets() |
| 82 | - gammalib::tmpnam() | 82 | - gammalib::tmpnam() |
| 83 | +- gammalib::compare() | ||
| 83 | 84 | ||
| 84 | The following methods have been removed: | 85 | The following methods have been removed: |
| 85 | - none | 86 | - none |
| @@ -209,6 +210,9 @@ Added GSkyRegionRect class to implement a rectangular region on the sky (#3248). | @@ -209,6 +210,9 @@ Added GSkyRegionRect class to implement a rectangular region on the sky (#3248). | ||
| 209 | 210 | ||
| 210 | 13. Support module | 211 | 13. Support module |
| 211 | ------------------ | 212 | ------------------ |
| 213 | +Added gammalib::compare() function to compare two floating point numbers with a | ||
| 214 | +relative tolerance (#3552). | ||
| 215 | + | ||
| 212 | Added gammalib::tmpnam() function to return a temporary file name, in replacement | 216 | Added gammalib::tmpnam() function to return a temporary file name, in replacement |
| 213 | of the deprecated str::tmpnam() function (#3324). | 217 | of the deprecated str::tmpnam() function (#3324). |
| 214 | 218 |
doc/source/admin/release_history/2.0.rst
| @@ -29,6 +29,8 @@ Bug fixes | @@ -29,6 +29,8 @@ Bug fixes | ||
| 29 | Improvements | 29 | Improvements |
| 30 | ------------ | 30 | ------------ |
| 31 | 31 | ||
| 32 | +* [`3552 <https://cta-redmine.irap.omp.eu/issues/3552>`_] - | ||
| 33 | + Add ``gammalib::compare()`` function | ||
| 32 | * Add DRI constructor to ``GCOMObservation`` class | 34 | * Add DRI constructor to ``GCOMObservation`` class |
| 33 | * Add member access methods to ``GCOMDri`` class | 35 | * Add member access methods to ``GCOMDri`` class |
| 34 | * [`3549 <https://cta-redmine.irap.omp.eu/issues/3549>`_] - | 36 | * [`3549 <https://cta-redmine.irap.omp.eu/issues/3549>`_] - |
include/GTools.hpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | * GTools.hpp - GammaLib tools * | 2 | * GTools.hpp - GammaLib tools * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2020 by Juergen Knoedlseder * | 4 | + * copyright (C) 2008-2021 by Juergen Knoedlseder * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
| @@ -151,6 +151,9 @@ namespace gammalib { | @@ -151,6 +151,9 @@ namespace gammalib { | ||
| 151 | const double& sindist, | 151 | const double& sindist, |
| 152 | const double& roi, | 152 | const double& roi, |
| 153 | const double& cosroi); | 153 | const double& cosroi); |
| 154 | + bool compare(const double& a, | ||
| 155 | + const double& b, | ||
| 156 | + const double& tol); | ||
| 154 | } | 157 | } |
| 155 | 158 | ||
| 156 | 159 |
pyext/GTools.i
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | * GTools.i - GammaLib tools * | 2 | * GTools.i - GammaLib tools * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2020 by Juergen Knoedlseder * | 4 | + * copyright (C) 2008-2021 by Juergen Knoedlseder * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
| @@ -106,4 +106,7 @@ namespace gammalib { | @@ -106,4 +106,7 @@ namespace gammalib { | ||
| 106 | const double& sindist, | 106 | const double& sindist, |
| 107 | const double& roi, | 107 | const double& roi, |
| 108 | const double& cosroi); | 108 | const double& cosroi); |
| 109 | + bool compare(const double& a, | ||
| 110 | + const double& b, | ||
| 111 | + const double& tol); | ||
| 109 | } | 112 | } |
src/support/GTools.cpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | * GTools.cpp - GammaLib tools * | 2 | * GTools.cpp - GammaLib tools * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2008-2020 by Juergen Knoedlseder * | 4 | + * copyright (C) 2008-2021 by Juergen Knoedlseder * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * 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, | @@ -1979,3 +1979,49 @@ double gammalib::roi_arclength(const double& rad, const double& dist, | ||
| 1979 | // Return arclength | 1979 | // Return arclength |
| 1980 | return arclength; | 1980 | return arclength; |
| 1981 | } | 1981 | } |
| 1982 | + | ||
| 1983 | + | ||
| 1984 | +/***********************************************************************//** | ||
| 1985 | + * @brief Compare two floating point values with tolerance | ||
| 1986 | + * | ||
| 1987 | + * @param[in] a First floating point value. | ||
| 1988 | + * @param[in] b Second floating point value. | ||
| 1989 | + * @param[in] tol Relative tolerance. | ||
| 1990 | + * @return True if both floating point values are identical within | ||
| 1991 | + * the tolerance. | ||
| 1992 | + * | ||
| 1993 | + * This method tests whether @p a is identical to @p b within a given | ||
| 1994 | + * relative tolerance @p tol. The test verifies whether | ||
| 1995 | + * | ||
| 1996 | + * \f[|a - b| \le |a \times tol|\f] | ||
| 1997 | + * | ||
| 1998 | + * for \f$a \ne 0\f$ or whether | ||
| 1999 | + * | ||
| 2000 | + * \f[|a - b| \le |b \times tol|\f] | ||
| 2001 | + * | ||
| 2002 | + * for \f$b \ne 0\f$. If both \f$a = 0\f$ and \f$b = 0\f$ the method | ||
| 2003 | + * returns true. | ||
| 2004 | + ***************************************************************************/ | ||
| 2005 | +bool gammalib::compare(const double& a, const double& b, const double& tol) | ||
| 2006 | +{ | ||
| 2007 | + // Initialise identity | ||
| 2008 | + bool identity = false; | ||
| 2009 | + | ||
| 2010 | + // Case A: a is not zero | ||
| 2011 | + if (a != 0.0) { | ||
| 2012 | + identity = std::abs(a - b) <= std::abs(a * tol); | ||
| 2013 | + } | ||
| 2014 | + | ||
| 2015 | + // Case B: b is not zero | ||
| 2016 | + else if (b != 0.0) { | ||
| 2017 | + identity = std::abs(a - b) <= std::abs(b * tol); | ||
| 2018 | + } | ||
| 2019 | + | ||
| 2020 | + // Case C: a and b are zero | ||
| 2021 | + else { | ||
| 2022 | + identity = true; | ||
| 2023 | + } | ||
| 2024 | + | ||
| 2025 | + // Return identity | ||
| 2026 | + return identity; | ||
| 2027 | +} |
test/test_GSupport.cpp
| 1 | /*************************************************************************** | 1 | /*************************************************************************** |
| 2 | * test_GSupport.cpp - test support module * | 2 | * test_GSupport.cpp - test support module * |
| 3 | * ----------------------------------------------------------------------- * | 3 | * ----------------------------------------------------------------------- * |
| 4 | - * copyright (C) 2010-2020 by Juergen Knoedlseder * | 4 | + * copyright (C) 2010-2021 by Juergen Knoedlseder * |
| 5 | * ----------------------------------------------------------------------- * | 5 | * ----------------------------------------------------------------------- * |
| 6 | * * | 6 | * * |
| 7 | * This program is free software: you can redistribute it and/or modify * | 7 | * This program is free software: you can redistribute it and/or modify * |
| @@ -147,6 +147,15 @@ void TestGSupport::test_tools(void) | @@ -147,6 +147,15 @@ void TestGSupport::test_tools(void) | ||
| 147 | test_assert(!gammalib::contains(strings, "b"), "\"b\" not in vector"); | 147 | test_assert(!gammalib::contains(strings, "b"), "\"b\" not in vector"); |
| 148 | test_assert(!gammalib::contains(strings, "isavector"), "\"isavector\" not in vector"); | 148 | test_assert(!gammalib::contains(strings, "isavector"), "\"isavector\" not in vector"); |
| 149 | 149 | ||
| 150 | + // Test compare method | ||
| 151 | + test_assert(gammalib::compare(261.0, 260.999999, 1.0e-8), "Compare positive floats for tol=1e-8"); | ||
| 152 | + test_assert(!gammalib::compare(261.0, 260.999999, 1.0e-9), "Compare positive floats for tol=1e-9"); | ||
| 153 | + test_assert(gammalib::compare(-261.0, -260.999999, 1.0e-8), "Compare negative floats for tol=1e-8"); | ||
| 154 | + test_assert(!gammalib::compare(-261.0, -260.999999, 1.0e-9), "Compare negative floats for tol=1e-9"); | ||
| 155 | + test_assert(gammalib::compare(0.0, 0.0000001, 1.0), "Compare near zero floats for tol=1.0"); | ||
| 156 | + test_assert(!gammalib::compare(0.0, 0.0000001, 0.1), "Compare near zero floats for tol=0.1"); | ||
| 157 | + test_assert(gammalib::compare(0.0, 0.0, 1.0e-10), "Compare zero floats for tol=1e-10"); | ||
| 158 | + | ||
| 150 | // Return | 159 | // Return |
| 151 | return; | 160 | return; |
| 152 | } | 161 | } |