From 9d28afa0125e870b8f03c018c30e375096868009 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 27 Jun 2021 18:56:05 +0900 Subject: [PATCH] fix: fix serialization of inf/nan --- toml/serializer.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/toml/serializer.hpp b/toml/serializer.hpp index 17ee951..3012e44 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -2,6 +2,7 @@ // Distributed under the MIT License. #ifndef TOML11_SERIALIZER_HPP #define TOML11_SERIALIZER_HPP +#include #include #include @@ -115,6 +116,29 @@ struct serializer } std::string operator()(const floating_type f) const { + if(std::isnan(f)) + { + if(std::signbit(f)) + { + return std::string("-nan"); + } + else + { + return std::string("nan"); + } + } + else if(!std::isfinite(f)) + { + if(std::signbit(f)) + { + return std::string("-inf"); + } + else + { + return std::string("inf"); + } + } + const auto fmt = "%.*g"; const auto bsz = std::snprintf(nullptr, 0, fmt, this->float_prec_, f); // +1 for null character(\0)