The output of the following code:
#include <limits>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <sstream>
using namespace std;
inline string lexical_cast(const float arg)
{
stringstream ss;
ss << fixed << setprecision(numeric_limits<float>::digits10) << arg;
if (!ss)
throw "Conversion failed";
return ss.str();
}
int main()
{
cout << numeric_limits<float>::digits10 << '\n';
cout << lexical_cast(32.123456789) << '\n';
}
is:
6
32.123455
I expected, and wanted:
6
32.1234
because, to the best of my knowledge, that's the extent of what a float
can reliably give me on my system.
How can I persuade IOStreams to behave as I want?