numeric_limits<double>::digits10
can be used to find the number of digits that are uniquely representable by a double
.
I see you've tagged your question with Visual Studio. You can test this code on http://webcompiler.cloudapp.net/ to get Visual Studio's number of uniquely representable digits for a double
:
#include <iostream>
#include <limits>
int main() { std::cout << std::numeric_limits<double>::digits10 << std::endl; }
This code will not output 2 it will output:
15
Which means that any double
up to 14 decimal places will survive the round trip through a stringstream
and still be equal to itself.
The above means that there is something you are not including in your example that is causing the round trip failure, or you are using non-standard source files that are not IEEE compliant. (For example I can do a live example on gcc that gives a contrary output to yours, and running the same code on Visual Studio disagrees with your output.)
Either way, for any uniquely representable double
(like 5.2), you can ensure that round trip success through a stringstream
by setting the precision. Precision is a sticky modifier, so you'll only need to set it once after stream construction. In your example you use stringstream stream
so before you work with stream
you'd need to set this modifier:
stream.precision(numeric_limits<double>::digits10 - 1);