Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yaml-cpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
yaml-cpp
Commits
de8253fc
Unverified
Commit
de8253fc
authored
Feb 15, 2020
by
Anton Onishchenko
Committed by
GitHub
Feb 14, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix storing inf and NaN (#817)
parent
29dcf92f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
1 deletions
+71
-1
include/yaml-cpp/node/convert.h
+17
-1
test/node/node_test.cpp
+54
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
de8253fc
...
...
@@ -8,10 +8,12 @@
#endif
#include <array>
#include <cmath>
#include <limits>
#include <list>
#include <map>
#include <sstream>
#include <type_traits>
#include <vector>
#include "yaml-cpp/binary.h"
...
...
@@ -94,7 +96,21 @@ struct convert<_Null> {
static Node encode(const type& rhs) { \
std::stringstream stream; \
stream.precision(std::numeric_limits<type>::max_digits10); \
stream << rhs; \
if (std::is_floating_point<type>::value) { \
if (std::isnan(rhs)) { \
stream << ".nan"; \
} else if (std::isinf(rhs)) { \
if (std::signbit(rhs)) { \
stream << "-.inf"; \
} else { \
stream << ".inf"; \
} \
} else { \
stream << rhs; \
} \
} else { \
stream << rhs; \
} \
return Node(stream.str()); \
} \
\
...
...
test/node/node_test.cpp
View file @
de8253fc
...
...
@@ -461,12 +461,66 @@ TEST(NodeTest, FloatingPrecisionFloat) {
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionPositiveInfinityFloat
)
{
if
(
!
std
::
numeric_limits
<
float
>::
has_infinity
)
{
return
;
}
const
float
x
=
std
::
numeric_limits
<
float
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNegativeInfinityFloat
)
{
if
(
!
std
::
numeric_limits
<
float
>::
has_infinity
)
{
return
;
}
const
float
x
=
-
std
::
numeric_limits
<
float
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNanFloat
)
{
if
(
!
std
::
numeric_limits
<
float
>::
has_quiet_NaN
)
{
return
;
}
const
float
x
=
std
::
numeric_limits
<
float
>::
quiet_NaN
();
Node
node
=
Node
(
x
);
EXPECT_TRUE
(
std
::
isnan
(
node
.
as
<
float
>
()));
}
TEST
(
NodeTest
,
FloatingPrecisionDouble
)
{
const
double
x
=
0.123456789
;
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
double
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionPositiveInfinityDouble
)
{
if
(
!
std
::
numeric_limits
<
double
>::
has_infinity
)
{
return
;
}
const
double
x
=
std
::
numeric_limits
<
double
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
float
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNegativeInfinityDouble
)
{
if
(
!
std
::
numeric_limits
<
double
>::
has_infinity
)
{
return
;
}
const
double
x
=
-
std
::
numeric_limits
<
double
>::
infinity
();
Node
node
=
Node
(
x
);
EXPECT_EQ
(
x
,
node
.
as
<
double
>
());
}
TEST
(
NodeTest
,
FloatingPrecisionNanDouble
)
{
if
(
!
std
::
numeric_limits
<
double
>::
has_quiet_NaN
)
{
return
;
}
const
double
x
=
std
::
numeric_limits
<
double
>::
quiet_NaN
();
Node
node
=
Node
(
x
);
EXPECT_TRUE
(
std
::
isnan
(
node
.
as
<
double
>
()));
}
TEST
(
NodeTest
,
SpaceChar
)
{
Node
node
=
Node
(
' '
);
EXPECT_EQ
(
' '
,
node
.
as
<
char
>
());
...
...
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