-
Notifications
You must be signed in to change notification settings - Fork 167
Description
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
isstd::errc::result_out_of_range
res.ptr
points to the end of the input stringvalue
is set to0
expected result
res.ec
is value-initializedres.ptr
points to the end of the input stringvalue
is set to0
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