npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
closeWithinTolerance.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_CLOSEWITHINTOLERANCE_HH_
2 #define NPSTAT_CLOSEWITHINTOLERANCE_HH_
3 
4 /*!
5 // \file closeWithinTolerance.hh
6 //
7 // \brief Determine if two doubles are within requested relative tolerance
8 // of each other
9 //
10 // Author: I. Volobouev
11 //
12 // July 2012
13 */
14 
15 #include <cmath>
16 #include <algorithm>
17 #include <stdexcept>
18 
19 namespace npstat {
20  /**
21  // Check if two doubles are within certain relative tolerance from
22  // each other. The "tol" argument which specifies the tolerance
23  // must be non-negative.
24  */
25  inline bool closeWithinTolerance(const double& a, const double& b,
26  const double& tol)
27  {
28  if (tol < 0.0)
29  throw std::invalid_argument("In npstat::closeWithinTolerance: "
30  "negative tolerance is not allowed");
31  if (a == b)
32  return true;
33  else
34  return fabs(a - b)/std::max(fabs(a), fabs(b)) <= tol;
35  }
36 }
37 
38 #endif // NPSTAT_CLOSEWITHINTOLERANCE_HH_
Definition: AbsArrayProjector.hh:14
bool closeWithinTolerance(const double &a, const double &b, const double &tol)
Definition: closeWithinTolerance.hh:25