From 6b4bc78fa6bfac2b2ad8b3f29bc74e0bda60b2a1 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Tue, 28 Feb 2023 10:36:23 -0800 Subject: [PATCH] Fix compute_float32 for edge cases --- .../boost/charconv/detail/compute_float32.hpp | 20 +++++++++++++------ src/from_chars.cpp | 4 ++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/include/boost/charconv/detail/compute_float32.hpp b/include/boost/charconv/detail/compute_float32.hpp index 503207f..b410c0c 100644 --- a/include/boost/charconv/detail/compute_float32.hpp +++ b/include/boost/charconv/detail/compute_float32.hpp @@ -6,22 +6,30 @@ #define BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT32_HPP #include -#include #include +#include namespace boost { namespace charconv { namespace detail { inline float compute_float32(std::int64_t power, std::uint64_t i, bool negative, bool& success) noexcept { const double d = compute_float64(power, i, negative, success); - - if (d > (std::numeric_limits::max)() || d < (std::numeric_limits::lowest)()) + float return_val; + + if (success) { - success = false; - return negative ? -0.0F : 0.0F; + return_val = static_cast(d); + if (std::isinf(return_val)) + { + return_val = negative ? -0.0F : 0.0F; + } + } + else + { + return_val = negative ? -0.0F : 0.0F; } - return static_cast(d); + return return_val; } }}} // Namespaces diff --git a/src/from_chars.cpp b/src/from_chars.cpp index aeb2ba9..7f7a1e3 100644 --- a/src/from_chars.cpp +++ b/src/from_chars.cpp @@ -37,7 +37,7 @@ boost::charconv::from_chars_result boost::charconv::from_chars(char const* first if (!success) { value = 0.0F; - r.ec = EINVAL; + r.ec = ERANGE; } else { @@ -65,7 +65,7 @@ boost::charconv::from_chars_result boost::charconv::from_chars(char const* first if (!success) { value = 0.0; - r.ec = EINVAL; + r.ec = ERANGE; } else {