Fix storing inf and NaN (#817)

This commit is contained in:
Anton Onishchenko
2020-02-15 03:03:21 +03:00
committed by GitHub
parent 29dcf92f87
commit de8253fcb0
2 changed files with 71 additions and 1 deletions

View File

@@ -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()); \
} \
\