Skip to content

Commit

Permalink
fixed #42 (removed equality comparisons for floats)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlohmann committed Mar 23, 2015
1 parent 8e1c5f2 commit 43417c3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions src/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1571,11 +1571,11 @@ class basic_json
{
if (rhs.type() == value_t::number_integer)
{
return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer);
return approx(lhs.m_value.number_float, static_cast<number_float_t>(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;
}
Expand Down Expand Up @@ -2008,6 +2008,13 @@ class basic_json
}
}

/// "equality" comparison for floating point numbers
template<typename T>
inline static bool approx(const T a, const T b)
{
return not (a > b or a < b);
}


private:
//////////////////////
Expand Down Expand Up @@ -4275,7 +4282,7 @@ class basic_json

// check if conversion loses precision
const auto int_val = static_cast<number_integer_t>(float_val);
if (float_val == int_val)
if (approx(float_val, static_cast<number_float_t>(int_val)))
{
// we basic_json not lose precision -> return int
return basic_json(int_val);
Expand Down
13 changes: 10 additions & 3 deletions src/json.hpp.re2c
Original file line number Diff line number Diff line change
Expand Up @@ -1571,11 +1571,11 @@ class basic_json
{
if (rhs.type() == value_t::number_integer)
{
return lhs.m_value.number_float == static_cast<number_float_t>(rhs.m_value.number_integer);
return approx(lhs.m_value.number_float, static_cast<number_float_t>(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;
}
Expand Down Expand Up @@ -2008,6 +2008,13 @@ class basic_json
}
}

/// "equality" comparison for floating point numbers
template<typename T>
inline static bool approx(const T a, const T b)
{
return not (a > b or a < b);
}


private:
//////////////////////
Expand Down Expand Up @@ -3624,7 +3631,7 @@ class basic_json

// check if conversion loses precision
const auto int_val = static_cast<number_integer_t>(float_val);
if (float_val == int_val)
if (approx(float_val, static_cast<number_float_t>(int_val)))
{
// we basic_json not lose precision -> return int
return basic_json(int_val);
Expand Down

0 comments on commit 43417c3

Please sign in to comment.