Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#ifndef DataFormats_Math_Rounding_h
#define DataFormats_Math_Rounding_h

// This file provides utilities for comparing and rounding floating point numbers

#include <cmath>

namespace cms_rounding {

  template <class valType>
  inline constexpr valType roundIfNear0(valType value, double tolerance = 1.e-7) {
    if (std::abs(value) < tolerance)
      return (0.0);
    return (value);
  }

  template <class valType>
  inline constexpr valType roundVecIfNear0(valType value, double tolerance = 1.e-7) {
    auto xVal{roundIfNear0(value.x(), tolerance)};
    auto yVal{roundIfNear0(value.y(), tolerance)};
    auto zVal{roundIfNear0(value.z(), tolerance)};
    return (valType{xVal, yVal, zVal});
  }

}  // namespace cms_rounding

#endif