B
bob
Hi,
this is driving me insane. I have 2 strings which I want to convert to
doubles. To this effect I have written a function;
// we'll ignore error handing for the moment
void stringToDouble( const std::string& chars, double& retVal )
{
std::stringstream str;
str << chars;
str >> retVal;
}
this code;
double d = 0;
std::string aDouble("12.12305000012");
::stringToFloat(aDouble,d);
cout << "d: " << d << endl;
double d2 = 0;
std::string aDouble2("1234.643434439");
::stringToFloat(aDouble2,d2);
cout << "d2: " << d2 << endl;
Produces this output;
d: 12.123050000119999
d2: 1234.643434439
d is obviously not what i put into the string. could anybody grace me
with details of;
1) what the issue is?
2) what can I do to have my "d" variable match precisely what i put
into the string. I've just cout'ed the results here but in real life
the value will be used in a mathematical function so i cant have
rounding/precision loss.
thanks much for any help
G
this is driving me insane. I have 2 strings which I want to convert to
doubles. To this effect I have written a function;
// we'll ignore error handing for the moment
void stringToDouble( const std::string& chars, double& retVal )
{
std::stringstream str;
str << chars;
str >> retVal;
}
this code;
double d = 0;
std::string aDouble("12.12305000012");
::stringToFloat(aDouble,d);
cout << "d: " << d << endl;
double d2 = 0;
std::string aDouble2("1234.643434439");
::stringToFloat(aDouble2,d2);
cout << "d2: " << d2 << endl;
Produces this output;
d: 12.123050000119999
d2: 1234.643434439
d is obviously not what i put into the string. could anybody grace me
with details of;
1) what the issue is?
2) what can I do to have my "d" variable match precisely what i put
into the string. I've just cout'ed the results here but in real life
the value will be used in a mathematical function so i cant have
rounding/precision loss.
thanks much for any help
G