Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:10:39

0001 #ifndef NPSTAT_CLOSEWITHINTOLERANCE_HH_
0002 #define NPSTAT_CLOSEWITHINTOLERANCE_HH_
0003 
0004 /*!
0005 // \file closeWithinTolerance.h
0006 //
0007 // \brief Determine if two doubles are within requested relative tolerance
0008 //        of each other
0009 //
0010 // Author: I. Volobouev
0011 //
0012 // July 2012
0013 */
0014 
0015 #include <cmath>
0016 #include <algorithm>
0017 #include "JetMETCorrections/InterpolationTables/interface/NpstatException.h"
0018 
0019 namespace npstat {
0020   /**
0021     // Check if two doubles are within certain relative tolerance from
0022     // each other. The "tol" argument which specifies the tolerance
0023     // must be non-negative.
0024     */
0025   inline bool closeWithinTolerance(const double& a, const double& b, const double& tol) {
0026     if (tol < 0.0)
0027       throw npstat::NpstatInvalidArgument(
0028           "In npstat::closeWithinTolerance: "
0029           "negative tolerance is not allowed");
0030     if (a == b)
0031       return true;
0032     else
0033       return fabs(a - b) / std::max(fabs(a), fabs(b)) <= tol;
0034   }
0035 }  // namespace npstat
0036 
0037 #endif  // NPSTAT_CLOSEWITHINTOLERANCE_HH_