From 43417c350b324e76ceac4d847f922d4fd92aef67 Mon Sep 17 00:00:00 2001 From: Niels Date: Mon, 23 Mar 2015 23:03:48 +0100 Subject: [PATCH] fixed #42 (removed equality comparisons for floats) --- Makefile | 2 +- src/json.hpp | 13 ++++++++++--- src/json.hpp.re2c | 13 ++++++++++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 105f4369..74074209 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ RE2C = re2c SED = gsed # additional flags -FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder -Wdeprecated +FLAGS = -Wall -Wextra -pedantic -Weffc++ -Wcast-align -Wcast-qual -Wctor-dtor-privacy -Wdisabled-optimization -Wformat=2 -Winit-self -Wmissing-declarations -Wmissing-include-dirs -Wold-style-cast -Woverloaded-virtual -Wredundant-decls -Wshadow -Wsign-conversion -Wsign-promo -Wstrict-overflow=5 -Wswitch -Wundef -Wno-unused -Wnon-virtual-dtor -Wreorder -Wdeprecated -Wfloat-equal all: json_unit diff --git a/src/json.hpp b/src/json.hpp index b845955f..82c389ab 100644 --- a/src/json.hpp +++ b/src/json.hpp @@ -1571,11 +1571,11 @@ class basic_json { if (rhs.type() == value_t::number_integer) { - return lhs.m_value.number_float == static_cast(rhs.m_value.number_integer); + return approx(lhs.m_value.number_float, static_cast(rhs.m_value.number_integer)); } if (rhs.type() == value_t::number_float) { - return lhs.m_value.number_float == rhs.m_value.number_float; + return approx(lhs.m_value.number_float, rhs.m_value.number_float); } break; } @@ -2008,6 +2008,13 @@ class basic_json } } + /// "equality" comparison for floating point numbers + template + inline static bool approx(const T a, const T b) + { + return not (a > b or a < b); + } + private: ////////////////////// @@ -4275,7 +4282,7 @@ basic_json_parser_59: // check if conversion loses precision const auto int_val = static_cast(float_val); - if (float_val == int_val) + if (approx(float_val, static_cast(int_val))) { // we basic_json not lose precision -> return int return basic_json(int_val); diff --git a/src/json.hpp.re2c b/src/json.hpp.re2c index 9d0a62cd..7b1c0d63 100644 --- a/src/json.hpp.re2c +++ b/src/json.hpp.re2c @@ -1571,11 +1571,11 @@ class basic_json { if (rhs.type() == value_t::number_integer) { - return lhs.m_value.number_float == static_cast(rhs.m_value.number_integer); + return approx(lhs.m_value.number_float, static_cast(rhs.m_value.number_integer)); } if (rhs.type() == value_t::number_float) { - return lhs.m_value.number_float == rhs.m_value.number_float; + return approx(lhs.m_value.number_float, rhs.m_value.number_float); } break; } @@ -2008,6 +2008,13 @@ class basic_json } } + /// "equality" comparison for floating point numbers + template + inline static bool approx(const T a, const T b) + { + return not (a > b or a < b); + } + private: ////////////////////// @@ -3624,7 +3631,7 @@ class basic_json // check if conversion loses precision const auto int_val = static_cast(float_val); - if (float_val == int_val) + if (approx(float_val, static_cast(int_val))) { // we basic_json not lose precision -> return int return basic_json(int_val);