https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81122
--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Ben Woodard from comment #4)
> Without this Is there a way to read and write floats and doubles accurately
> without the rounding that converting to/from base 10 ends up introducing?
> How are you supposed to use istreams like
>
> printf(“%a”,dbl);
std::cout << std::hexfloat << dbl;
> scanf(“%f, &dbl);
You can't read a hexadecimal float back in using an istream.
C++17 provides std::to_chars and std::from_chars for lossless conversion of
numbers to/from strings, but GCC only supports it for integers so far, not
floats.
> Note that scanf does handle doubles in whatever form they come in.
As do strtof, strtod and strtold.
> Further note that according to stackexchange this works on llvm and other
> libstdc++ implementations.
N.B. There are no other libstdc++ implementations, libstdc++ is the name of
GCC's C++ standard library implementation, not the generic name.
Some cases work with LLVM's libc++, but their support also gives the wrong
result for valid cases that don't involve hex floats:
#include <sstream>
#include <cassert>
int main()
{
double d;
char c;
std::istringstream in("1.00f");
in >> d >> c;
assert( d == 1.0 && c == 'f' );
}
This fails with libc++.
> Therefore if the standard is unclear in this area
> maybe it is a topic to be clarified.
See comment 1.