EqualWithinRoundoff.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <algorithm>
7 #include <limits>
8 
11 
12 /// \ingroup UtilitiesGroup
13 /// Checks if two values `a` and `b` are equal within roundoff,
14 /// by comparing `abs(a - b) < (max(abs(a), abs(b)) + scale) * eps`.
16  const double a, const double b,
17  const double eps = std::numeric_limits<double>::epsilon() * 100.0,
18  const double scale = 1.0) noexcept {
19  return ce_fabs(a - b) < (std::max(ce_fabs(a), ce_fabs(b)) + scale) * eps;
20 }
#define SPECTRE_ALWAYS_INLINE
Always inline a function. Only use this if you benchmarked the code.
Definition: ForceInline.hpp:20
Define simple functions for constant expressions.
Defines macro to always inline a function.
constexpr double ce_fabs(const double x) noexcept
Compute the absolute value of its argument.
Definition: ConstantExpressions.hpp:187
constexpr bool equal_within_roundoff(const double a, const double b, const double eps=std::numeric_limits< double >::epsilon() *100.0, const double scale=1.0) noexcept
Checks if two values a and b are equal within roundoff, by comparing abs(a - b) < (max(abs(a)...
Definition: EqualWithinRoundoff.hpp:15