Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
43417c35
Commit
43417c35
authored
Mar 23, 2015
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed #42 (removed equality comparisons for floats)
parent
8e1c5f22
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
7 deletions
+21
-7
Makefile
Makefile
+1
-1
src/json.hpp
src/json.hpp
+10
-3
src/json.hpp.re2c
src/json.hpp.re2c
+10
-3
No files found.
Makefile
View file @
43417c35
...
...
@@ -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
...
...
src/json.hpp
View file @
43417c35
...
...
@@ -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
;
}
...
...
@@ -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:
//////////////////////
...
...
@@ -4275,7 +4282,7 @@ basic_json_parser_59:
// 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
);
...
...
src/json.hpp.re2c
View file @
43417c35
...
...
@@ -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;
}
...
...
@@ -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:
//////////////////////
...
...
@@ -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);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment