Lauri Kaila
|
string-double conversion failed in C++
|
Lauri Kaila
03/13/2018 8:37 AM
post118663
|
string-double conversion failed in C++
Hi all,
Shouldn't it be possible to convert max double value to string and then back to double? In this example, std::
istringstream fails to do that (works in Linux):
#include <iostream>
#include <sstream>
#include <limits>
#include <string>
#include <cfloat>
const double d = std::numeric_limits<double>::max();
int main()
{
std::cout << "double max: " << d << "\n";
std::string tst = std::to_string(d);
std::cout << "std::to_string: " << tst << "\n";
std::istringstream ss(tst);
double result;
ss >> result;
if (ss.fail()) {
std::cout << "Failed to convert back to double.\n";
return 1;
} else {
std::cout << "Conversion OK.\n";
}
return 0;
}
Compiled with:
QCC -V5.4.0,gcc_ntox86_64_gpp -g -Wall -Wextra -std=c++14 float1.cpp -o float1
|
|
|