fuzzy floating point comparison with optional

This commit is contained in:
Crzyrndm 2018-08-18 16:31:56 +12:00
parent 3db87244f1
commit 19aad52500

View File

@ -25,6 +25,7 @@
#include <xlnt/xlnt_config.hpp>
#include <xlnt/utils/exceptions.hpp>
#include <detail/numeric_utils.hpp>
#include <type_traits>
namespace xlnt {
@ -49,6 +50,23 @@ class optional
using set_move_noexcept_t = typename std::conditional<std::is_nothrow_move_constructible<T>{} && std::is_nothrow_move_assignable<T>{}, std::true_type, std::false_type>::type;
using clear_noexcept_t = typename std::conditional<std::is_nothrow_destructible<T>{}, std::true_type, std::false_type>::type;
#endif
/// <summary>
/// Default equality operation, just uses operator==
/// </summary>
template <typename T, typename std::enable_if<!std::is_floating_point_v<T>>::type * = nullptr>
constexpr bool compare_equal(const T &lhs, const T &rhs) const
{
return lhs == rhs;
}
/// <summary>
/// equality operation for floating point numbers. Provides "fuzzy" equality
/// </summary>
template <typename T, typename std::enable_if<std::is_floating_point_v<T>>::type * = nullptr>
constexpr bool compare_equal(const T &lhs, const T &rhs) const
{
return detail::float_equals(lhs, rhs);
}
public:
/// <summary>
@ -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());
}
/// <summary>