utils/FloatCompare.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // <FloatCompare.hpp> -*- C++ -*- | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <cinttypes> | ||
| 6 | #include <cmath> | ||
| 7 | #include <limits> | ||
| 8 | #include <random> | ||
| 9 | #include <time.h> | ||
| 10 | |||
| 11 | namespace simdb { | ||
| 12 | |||
| 13 | /// Comparison of two floating-point values with | ||
| 14 | /// a supplied tolerance. The tolerance value defaults | ||
| 15 | /// to machine epsilon. | ||
| 16 | template <typename T> | ||
| 17 | typename std::enable_if<std::is_floating_point<T>::value, bool>::type | ||
| 18 | 328 | approximatelyEqual(const T a, const T b, const T epsilon = std::numeric_limits<T>::epsilon()) | |
| 19 | { | ||
| 20 | 328 | const T fabs_a = std::fabs(a); | |
| 21 | 328 | const T fabs_b = std::fabs(b); | |
| 22 | 328 | const T fabs_diff = std::fabs(a - b); | |
| 23 | |||
| 24 | 328 | return fabs_diff <= ((fabs_a < fabs_b ? fabs_b : fabs_a) * epsilon); | |
| 25 | } | ||
| 26 | |||
| 27 | } // namespace simdb | ||
| 28 |