https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123078
Bug ID: 123078
Summary: std::from_chars handles signedness of zero in
underflow inconsistently with floating-point literals
Product: gcc
Version: 16.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: janschultke at googlemail dot com
Target Milestone: ---
Consider the following code (https://godbolt.org/z/cjndGqbbT):
#include <charconv>
#include <string_view>
#include <iostream>
#include <cmath>
int main() {
constexpr char s[] = "-1e-1000";
float x;
const auto result = std::from_chars(s, std::end(s), x);
// x = -1e-1000;
std::cout << x;
}
libstdc++ prints "0", whereas libc++ and MSVC STL print "-0".
Also, the handling of negative zero is inconsistent with the spelling of
floating-point literals. All compilers produce a negative zero for the
uncommented line, which is actually a negated zero obtained from the "1e-1000"
floating-point literal with positive value.
Strictly speaking, I don't think this is a compliance bug because the standard
doesn't mandate a zero sign for std::from_chars, but I don't think this design
is intentional. Keep in mind that the string "-0" produces a negative zero for
libstdc++, meaning that a leading "-" character inconsistently affects the sign
of the result.