From e0747e4ba690910352468d5fb01295d4f6b9d66a Mon Sep 17 00:00:00 2001 From: Tuomo Kriikkula Date: Wed, 22 Nov 2023 02:43:51 +0200 Subject: [PATCH] Minor float encoding fixes --- include/umb/coding.hpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/include/umb/coding.hpp b/include/umb/coding.hpp index 756bcf1..cd3b03b 100644 --- a/include/umb/coding.hpp +++ b/include/umb/coding.hpp @@ -503,12 +503,14 @@ inline UMB_CONSTEXPR void encode_float(float f, std::string& out) { std::string str; - // NOTE: using double here to ensure we reserve enough to fit all floats. - constexpr auto limit = std::numeric_limits::max_digits10 + 64; - // Reserve extra for special chars like +-e. - str.resize(limit + 8); + // TODO: consider this precision. Does it even make sense? + constexpr auto pre = 64; + constexpr auto longest_float = std::numeric_limits::digits + - std::numeric_limits::min_exponent; + constexpr auto max_str = longest_float + 8; + str.resize(max_str); const auto fmt = f < 0 ? std::chars_format::fixed : std::chars_format::scientific; - const auto [ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), f, fmt, limit); + const auto [ptr, ec] = std::to_chars(str.data(), str.data() + str.size(), f, fmt, pre); if (ec == std::errc()) { out = std::move(str);