Fix compute_float32 for edge cases

This commit is contained in:
Matt Borland
2023-02-28 10:36:23 -08:00
parent 513e7e6662
commit 6b4bc78fa6
2 changed files with 16 additions and 8 deletions

View File

@@ -6,22 +6,30 @@
#define BOOST_CHARCONV_DETAIL_COMPUTE_FLOAT32_HPP
#include <boost/charconv/detail/compute_float64.hpp>
#include <limits>
#include <cstdint>
#include <cmath>
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<float>::max)() || d < (std::numeric_limits<float>::lowest)())
float return_val;
if (success)
{
success = false;
return negative ? -0.0F : 0.0F;
return_val = static_cast<float>(d);
if (std::isinf(return_val))
{
return_val = negative ? -0.0F : 0.0F;
}
}
else
{
return_val = negative ? -0.0F : 0.0F;
}
return static_cast<float>(d);
return return_val;
}
}}} // Namespaces

View File

@@ -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
{