Version: SMASH-3.1
numerics.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2018,2020,2022-2023
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_NUMERICS_H_
11 #define SRC_INCLUDE_SMASH_NUMERICS_H_
12 
13 #include <cmath>
14 #include <initializer_list>
15 
16 #include "constants.h"
17 
27 namespace smash {
43 template <typename N>
44 bool almost_equal(const N x, const N y) {
45  return (std::abs(x - y) <= N(really_small) ||
46  std::abs(x - y) <=
47  N(0.5 * really_small) * (std::abs(x) + std::abs(y)));
48 }
63 template <typename N>
64 bool almost_equal_physics(const N x, const N y) {
65  return (std::abs(x - y) <= N(small_number) ||
66  std::abs(x - y) <=
67  N(0.5 * small_number) * (std::abs(x) + std::abs(y)));
68 }
69 
81 template <typename T = std::initializer_list<double>>
82 bool is_any_nan(const T& collection) {
83  for (const auto& number : collection) {
84  if (unlikely(std::isnan(number)))
85  return true;
86  }
87  return false;
88 }
89 
90 } // namespace smash
91 
92 #endif // SRC_INCLUDE_SMASH_NUMERICS_H_
Collection of useful constants that are known at compile time.
#define unlikely(x)
Tell the branch predictor that this expression is likely false.
Definition: macros.h:16
Definition: action.h:24
bool almost_equal_physics(const N x, const N y)
Same as smash::almost_equal, but for physical checks like energy-momentum conservation small_number i...
Definition: numerics.h:64
constexpr double small_number
Physical error tolerance.
Definition: constants.h:51
bool is_any_nan(const T &collection)
This function iterates through the elements of a collection and checks if any of them is NaN using th...
Definition: numerics.h:82
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37
bool almost_equal(const N x, const N y)
Checks two numbers for relative approximate equality.
Definition: numerics.h:44