10 #ifndef SRC_INCLUDE_SMASH_NUMERICS_H_
11 #define SRC_INCLUDE_SMASH_NUMERICS_H_
16 #include <initializer_list>
18 #include <type_traits>
58 template <
typename N,
typename = std::enable_if_t<std::is_
floating_po
int_v<N>>>
60 const N threshold = N{0.0}) noexcept {
62 assert(threshold >= 0);
63 if constexpr (std::numeric_limits<N>::is_iec559) {
64 if (!std::isfinite(x) || !std::isfinite(y)) {
71 return std::abs(y) <= epsilon;
73 return std::abs(x) <= epsilon;
75 return std::abs(x - y) <= threshold ||
76 std::abs(x - y) <= epsilon * std::max(std::abs(x), std::abs(y));
88 template <
typename N,
typename = std::enable_if_t<std::is_
floating_po
int_v<N>>>
90 return detail::almost_equal_knuthish<N>(x, y,
static_cast<N
>(
really_small));
99 template <
typename N,
typename = std::enable_if_t<std::is_
floating_po
int_v<N>>>
103 return detail::almost_equal_knuthish<N>(x, y, epsilon, threshold);
120 template <
typename T = std::initializer_list<
double>>
122 for (
const auto& number : collection) {
Collection of useful constants that are known at compile time.
#define unlikely(x)
Tell the branch predictor that this expression is likely false.
bool almost_equal_knuthish(const N x, const N y, const N epsilon, const N threshold=N{0.0}) noexcept
Compare whether two floating-point numbers are approximately equal à la Knuth up to a given tolerance...
bool almost_equal(const N x, const N y)
Checks whether two floating-point numbers are almost equal.
constexpr double small_number
Physical error tolerance.
bool is_any_nan(const T &collection)
Returns whether any element in a collection is NaN.
bool almost_equal_physics(const N x, const N y)
Like smash::almost_equal, but using a less strict tolerance, smash::small_number.
constexpr double really_small
Numerical error tolerance.