I loose some precision when converting string to float.
Feb 28, 2012 at 2:30am UTC
Hello,
When I convert a string to float I get a value that is a little smaller. e.g. as in the code below I expect price to be 1.3467800 (which std::cout outputs correctly) but it's actually 1.3467994.
Does someone know what's going? -- THX!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
#include<iostream>
#include<string>
#include<sstream> // stringstream
using namespace std;
double my_stod(std::string& s);
int main()
{
string str = "1.34678" ;
float price = my_stod(str);
float price2 = atof(str.c_str());
cout << "price1 = " << price <<
" price2 = " << price2 << endl << flush;
return 0;
}
double my_stod(std::string& s)
{
std::istringstream stream(s);
double i;
stream >> i;
return i;
}
Feb 28, 2012 at 12:48pm UTC
Feb 29, 2012 at 11:29am UTC
Topic archived. No new replies allowed.