ATLAS Offline Software
Loading...
Searching...
No Matches
close_to_zero.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
10
11#ifndef CXXUTILS_CLOSE_TO_ZERO_H
12#define CXXUTILS_CLOSE_TO_ZERO_H
13
14#include <limits> //for epsilon
15#include <cmath> //for abs
16#include <type_traits> //for std::is_integral_v etc
17
18
19namespace CxxUtils {
47 template <typename T>
48 bool close_to_zero(T value, T eps = std::numeric_limits<T>::epsilon()) {
49 if constexpr (std::is_integral_v<T>) {
50 // For integers, only exact zero is invalid
51 return value == 0;
52 } else if constexpr (std::is_floating_point_v<T>) {
53 return std::abs(value) < eps;
54 } else {
55 static_assert(std::is_arithmetic_v<T>, "close_to_zero: Only arithmetic types supported");
56 return false;
57 }
58 }
59}
60
61#endif
62
bool close_to_zero(T value, T eps=std::numeric_limits< T >::epsilon())