From 19aad525006f5cd57e8bb598d475e68eb0ab10f4 Mon Sep 17 00:00:00 2001 From: Crzyrndm Date: Sat, 18 Aug 2018 16:31:56 +1200 Subject: [PATCH] fuzzy floating point comparison with optional --- include/xlnt/utils/optional.hpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/include/xlnt/utils/optional.hpp b/include/xlnt/utils/optional.hpp index 8677f24b..4f56c812 100644 --- a/include/xlnt/utils/optional.hpp +++ b/include/xlnt/utils/optional.hpp @@ -25,6 +25,7 @@ #include #include +#include #include namespace xlnt { @@ -49,6 +50,23 @@ class optional using set_move_noexcept_t = typename std::conditional{} && std::is_nothrow_move_assignable{}, std::true_type, std::false_type>::type; using clear_noexcept_t = typename std::conditional{}, std::true_type, std::false_type>::type; #endif + /// + /// Default equality operation, just uses operator== + /// + template >::type * = nullptr> + constexpr bool compare_equal(const T &lhs, const T &rhs) const + { + return lhs == rhs; + } + + /// + /// equality operation for floating point numbers. Provides "fuzzy" equality + /// + template >::type * = nullptr> + constexpr bool compare_equal(const T &lhs, const T &rhs) const + { + return detail::float_equals(lhs, rhs); + } public: /// @@ -281,7 +299,8 @@ public: { return true; } - return value_ref() == other.value_ref(); + // equality is overloaded to provide fuzzy equality when T is a fp number + return compare_equal(value_ref(), other.value_ref()); } ///