From 3a1403409fc12d042608b381e891ca377de300b6 Mon Sep 17 00:00:00 2001 From: Trevor Welsby Date: Sat, 23 Jan 2016 17:45:30 +1000 Subject: [PATCH] Issue #185 - remove approx() and use #pragma to kill warnings --- src/json.hpp | 27 +++++++++++++++------------ src/json.hpp.re2c | 27 +++++++++++++++------------ test/unit.cpp | 5 +++++ 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/json.hpp b/src/json.hpp index ac362dcf..3af7dbf9 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -65,6 +65,12 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #endif #endif +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + // enable ssize_t for MSVC #ifdef _MSC_VER #include @@ -100,12 +106,6 @@ struct has_mapped_type static constexpr bool value = sizeof(test(0)) == 1; }; -/// "equality" comparison for floating point numbers -template -static bool approx(const T a, const T b) -{ - return not (a > b or a < b); -} } /*! @@ -4785,7 +4785,7 @@ class basic_json } case value_t::number_float: { - return approx(lhs.m_value.number_float, rhs.m_value.number_float); + return lhs.m_value.number_float == rhs.m_value.number_float; } default: { @@ -4795,13 +4795,11 @@ class basic_json } else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float) { - return approx(static_cast(lhs.m_value.number_integer), - rhs.m_value.number_float); + return static_cast(lhs.m_value.number_integer == rhs.m_value.number_float); } else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer) { - return approx(lhs.m_value.number_float, - static_cast(rhs.m_value.number_integer)); + return lhs.m_value.number_float == static_cast(rhs.m_value.number_integer); } return false; } @@ -7560,7 +7558,7 @@ basic_json_parser_64: // check if conversion loses precision const auto int_val = static_cast(float_val); - if (approx(float_val, static_cast(int_val))) + if (float_val == static_cast(int_val)) { // we would not lose precision -> return int result.m_type = value_t::number_integer; @@ -7705,4 +7703,9 @@ inline nlohmann::json operator "" _json(const char* s, std::size_t) return nlohmann::json::parse(reinterpret_cast(s)); } +// restore GCC/clang diagnostic settings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic pop +#endif + #endif diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index c7ee44f5..bacc3feb 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -65,6 +65,12 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #endif #endif +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + // enable ssize_t for MSVC #ifdef _MSC_VER #include @@ -100,12 +106,6 @@ struct has_mapped_type static constexpr bool value = sizeof(test(0)) == 1; }; -/// "equality" comparison for floating point numbers -template -static bool approx(const T a, const T b) -{ - return not (a > b or a < b); -} } /*! @@ -4785,7 +4785,7 @@ class basic_json } case value_t::number_float: { - return approx(lhs.m_value.number_float, rhs.m_value.number_float); + return lhs.m_value.number_float == rhs.m_value.number_float; } default: { @@ -4795,13 +4795,11 @@ class basic_json } else if (lhs_type == value_t::number_integer and rhs_type == value_t::number_float) { - return approx(static_cast(lhs.m_value.number_integer), - rhs.m_value.number_float); + return static_cast(lhs.m_value.number_integer == rhs.m_value.number_float); } else if (lhs_type == value_t::number_float and rhs_type == value_t::number_integer) { - return approx(lhs.m_value.number_float, - static_cast(rhs.m_value.number_integer)); + return lhs.m_value.number_float == static_cast(rhs.m_value.number_integer); } return false; } @@ -7242,7 +7240,7 @@ class basic_json // check if conversion loses precision const auto int_val = static_cast(float_val); - if (approx(float_val, static_cast(int_val))) + if (float_val == static_cast(int_val)) { // we would not lose precision -> return int result.m_type = value_t::number_integer; @@ -7387,4 +7385,9 @@ inline nlohmann::json operator "" _json(const char* s, std::size_t) return nlohmann::json::parse(reinterpret_cast(s)); } +// restore GCC/clang diagnostic settings +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic pop +#endif + #endif diff --git a/test/unit.cpp b/test/unit.cpp index 8b3bc19b..8f12eca0 100644 --- a/test/unit.cpp +++ b/test/unit.cpp @@ -25,6 +25,11 @@ #include "json.hpp" using nlohmann::json; +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) +#pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + TEST_CASE("constructors") { SECTION("create an empty value with a given type")