Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supress -Wfloat-equal on intended float comparisions #2911

Merged
merged 3 commits into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/nlohmann/detail/conversions/to_chars.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,8 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '-';
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (value == 0) // +-0
{
*first++ = '0';
Expand All @@ -1074,6 +1076,7 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '0';
return first;
}
#pragma GCC diagnostic pop

JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);

Expand Down
3 changes: 3 additions & 0 deletions include/nlohmann/detail/output/binary_writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,8 @@ class binary_writer

void write_compact_float(const number_float_t n, detail::input_format_t format)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
Expand All @@ -1540,6 +1542,7 @@ class binary_writer
: get_msgpack_float_prefix(n));
write_number(n);
}
#pragma GCC diagnostic pop
}

public:
Expand Down
3 changes: 3 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6278,6 +6278,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
*/
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
const auto lhs_type = lhs.type();
const auto rhs_type = rhs.type();

Expand Down Expand Up @@ -6342,6 +6344,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}

return false;
#pragma GCC diagnostic pop
}

/*!
Expand Down
9 changes: 9 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14825,6 +14825,8 @@ class binary_writer

void write_compact_float(const number_float_t n, detail::input_format_t format)
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (static_cast<double>(n) >= static_cast<double>(std::numeric_limits<float>::lowest()) &&
static_cast<double>(n) <= static_cast<double>((std::numeric_limits<float>::max)()) &&
static_cast<double>(static_cast<float>(n)) == static_cast<double>(n))
Expand All @@ -14841,6 +14843,7 @@ class binary_writer
: get_msgpack_float_prefix(n));
write_number(n);
}
#pragma GCC diagnostic pop
}

public:
Expand Down Expand Up @@ -15981,6 +15984,8 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '-';
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
if (value == 0) // +-0
{
*first++ = '0';
Expand All @@ -15989,6 +15994,7 @@ char* to_chars(char* first, const char* last, FloatType value)
*first++ = '0';
return first;
}
#pragma GCC diagnostic pop

JSON_ASSERT(last - first >= std::numeric_limits<FloatType>::max_digits10);

Expand Down Expand Up @@ -23363,6 +23369,8 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
*/
friend bool operator==(const_reference lhs, const_reference rhs) noexcept
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wfloat-equal"
const auto lhs_type = lhs.type();
const auto rhs_type = rhs.type();

Expand Down Expand Up @@ -23427,6 +23435,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}

return false;
#pragma GCC diagnostic pop
}

/*!
Expand Down
1 change: 0 additions & 1 deletion test/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ SOFTWARE.
*/

#include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")

#include <nlohmann/json.hpp>
using nlohmann::json;
Expand Down
1 change: 0 additions & 1 deletion test/src/unit-constructor1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ SOFTWARE.
*/

#include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")

#define JSON_TESTS_PRIVATE
#include <nlohmann/json.hpp>
Expand Down
1 change: 0 additions & 1 deletion test/src/unit-readme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ SOFTWARE.
*/

#include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")

#include <nlohmann/json.hpp>
using nlohmann::json;
Expand Down
1 change: 0 additions & 1 deletion test/src/unit-reference_access.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ SOFTWARE.
*/

#include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")

#include <nlohmann/json.hpp>
using nlohmann::json;
Expand Down
1 change: 0 additions & 1 deletion test/src/unit-regression1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ SOFTWARE.
*/

#include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")

// for some reason including this after the json header leads to linker errors with VS 2017...
#include <locale>
Expand Down
1 change: 0 additions & 1 deletion test/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ SOFTWARE.
*/

#include "doctest_compatibility.h"
DOCTEST_GCC_SUPPRESS_WARNING("-Wfloat-equal")

// for some reason including this after the json header leads to linker errors with VS 2017...
#include <locale>
Expand Down