Volume Cartographer 2.27.0
FloatComparison.hpp
Go to the documentation of this file.
1#pragma once
2
19#include <algorithm>
20#include <cmath>
21#include <limits>
22#include <type_traits>
23
24namespace volcart
25{
26
27static constexpr double DEFAULT_MAX_DIFFERENCE = 1e-7;
28
46template <
47 typename T,
48 std::enable_if_t<std::is_floating_point<T>::value, bool> = true>
49inline auto AlmostEqual(
50 const T lhs,
51 const T rhs,
52 T epsAbs = static_cast<T>(DEFAULT_MAX_DIFFERENCE),
53 T epsRel = std::numeric_limits<T>::epsilon()) -> bool
54{
55 // Note: For some reason, std::abs resolves to integral overload? - SP
56 T d = std::fabs(lhs - rhs);
57 if (d <= epsAbs) {
58 return true;
59 }
60 T l = std::fabs(lhs);
61 T r = std::fabs(rhs);
62
63 T largest = std::max(r, l);
64 return d <= largest * epsRel;
65}
66
67} // namespace volcart
Volume Cartographer library
auto AlmostEqual(const T lhs, const T rhs, T epsAbs=static_cast< T >(DEFAULT_MAX_DIFFERENCE), T epsRel=std::numeric_limits< T >::epsilon()) -> bool
Compare if two floating-point numbers are "almost equal".