Skip to content

result modified when result_out_of_range #261

@igagis

Description

@igagis

the code

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);

std::cout << "value = " << value << std::endl; // prints "value = 0"

assert(res.ptr == str.data() + str.size()); // assertion succeeds

assert(res.ec != std::errc::result_out_of_range); // assertion fails

actual result

  • res.ec is std::errc::result_out_of_range
  • res.ptr points to the end of the input string
  • value is set to 0

expected result

  • res.ec is value-initialized
  • res.ptr points to the end of the input string
  • value is set to 0

justification

According to https://en.cppreference.com/w/cpp/utility/from_chars

In any case, the resulting value is one of at most two floating-point values closest to the value of the string matching the pattern, after rounding according to std::round_to_nearest.

On success, returns a value of type std::from_chars_result such that ptr points at the first character not matching the pattern, or has the value equal to last if all characters match and ec is value-initialized.

If the pattern was matched, but the parsed value is not in the range representable by the type of value, returns value of type std::from_chars_result such that ec equals std::errc::result_out_of_range and ptr points at the first character not matching the pattern. value is unmodified.

So, from (1) and (2) we can say that the actual value of the string falls between 0 and smallest positive number which float can represent, perhaps closest one is 0. So, the output value should be set to 0 and error code value-initialized.

Even if the actual value of the string is considered to be out of range representable by float (which I think it is not) then the value should be unmodified, according to (3), and error code is result_out_of_range. But in this test the value is set to 0.

version

Found in version: 6.4.1
Last version where it worked as expected was: 3.11.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions