Re: Read NaN from stream?
Am 08.09.2011 21:19, schrieb Helmut Zeisel:
I want to write strings and floats to a stream and later want to read
the data from the stream again.
Some of the floats are NaN; if they are written, they normal parsing
will not work any longer. What is the usual way to solve this?
See code below, at the end of the program there should be d='D', but
actually it is "#" (with VC++).
double a = 0;
char b = 'B';
double c = a/a;
char d = 'D';
s<< a<< " "<< b<< " "<< c<< " "<< d;
std::cout<< " s = "<< s.str()<< std::endl;
s>> a>> b>> c>> d;
std::cout<< " a = "<< a<< std::endl;
std::cout<< " b = "<< b<< std::endl;
std::cout<< " c = "<< c<< std::endl;
std::cout<< " d = "<< d<< std::endl;
I agree that your serialization round-trip should work in principle, but
unfortunately the state of the standard (even for C++11) does still not
support NaN nor infinity deserialization. There is one issue in
But the current wording suggestion does still not allow for non-finite
value deserialization (But serialization, which is funny). The actual
problem is the specification of stage 2 in [facet.num.get.virtuals] p3:
The accepted character atoms are restricted to the set:
"0123456789abcdefxABCDEFX+-", which does not include letters to form nan
and inf (and not nan(n-char-sequenceopt) for that matter). Given the
uncertainty of the actual resolution of this issue I recommend to use
the C99 API directly after having read a std::string value.
HTH & Greetings from Bremen,
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]