diff --git a/include/fast_float/parse_number.h b/include/fast_float/parse_number.h index 6d883fb..5356122 100644 --- a/include/fast_float/parse_number.h +++ b/include/fast_float/parse_number.h @@ -199,7 +199,7 @@ from_chars(UC const *first, UC const *last, T &value, template FASTFLOAT_CONSTEXPR20 from_chars_result_t from_chars_advanced(parsed_number_string_t &pns, T &value) noexcept { - +printf("from_chars_advanced\n"); static_assert(is_supported_float_type(), "only some floating-point types are supported"); static_assert(is_supported_char_type(), @@ -275,11 +275,12 @@ from_chars_advanced(parsed_number_string_t &pns, T &value) noexcept { if (am.power2 < 0) { am = digit_comp(pns, am); } - to_float(pns.negative, am, value); // Test for over/underflow. if ((pns.mantissa != 0 && am.mantissa == 0 && am.power2 == 0) || am.power2 == binary_format::infinite_power()) { answer.ec = std::errc::result_out_of_range; + } else { + to_float(pns.negative, am, value); } return answer; } diff --git a/tests/basictest.cpp b/tests/basictest.cpp index a66ccaf..60046ac 100644 --- a/tests/basictest.cpp +++ b/tests/basictest.cpp @@ -76,6 +76,15 @@ const char *round_name(int d) { #define FASTFLOAT_STR(x) #x #define SHOW_DEFINE(x) printf("%s='%s'\n", #x, FASTFLOAT_STR(x)) +TEST_CASE("issue261") { + std::string_view str = "5.47382e-48"; + float value = 100.0f; + auto res = fast_float::from_chars(str.data(), str.data() + str.size(), value, fast_float::general); + CHECK_MESSAGE(res.ec == std::errc::result_out_of_range, "value is out of range (interpretation of the standard)"); + std::cout << "value = " << fHexAndDec(value) << std::endl; + CHECK_MESSAGE(value == 100.0f, "value should be unchanged"); + CHECK_MESSAGE(res.ptr == str.data() + str.size(), "should point to end of matching pattern"); +} TEST_CASE("system_info") { std::cout << "system info:" << std::endl; #ifdef FASTFLOAT_CONSTEXPR_TESTS @@ -400,6 +409,8 @@ TEST_CASE("full_multiplication") { test_full_multiplication(bit << 63, bit << 63, 0u, bit << 62); } + + TEST_CASE("issue8") { const char *s = "3."