atof() losing precision
-
When numbers are larger than 15 significant figures the function atof() is losing precision. For example:
double dVal = atof( pszValue ); // where pszValue holds the string "1111111111111111"
dVal ends up with 1.11111111111111e+15 which equates to: 1111111111111110 Can anyone suggest a safer way to convert the char* to a double? -
When numbers are larger than 15 significant figures the function atof() is losing precision. For example:
double dVal = atof( pszValue ); // where pszValue holds the string "1111111111111111"
dVal ends up with 1.11111111111111e+15 which equates to: 1111111111111110 Can anyone suggest a safer way to convert the char* to a double?That's because atof returns a float (guess what the 'f' in the function name stands for ?). You could try by using stringstream from the STL:
stringstream ssVal;
double dValue;
ssVal << pszValue;
ssVal >> dValue;Don't forget to
#include <sstream>
and to putusing namespace std;
at the top of your cpp file. Forget what I say, I wasn't awake ;P . If you look at the range of a double, you'll see that the accuracy (here)[^] is 14 or 15 digits. Your string contains much more digits than that. Now, if the number that you want to represent is an integer value, why don't you use an __int64 instead ? But, I have a question: why do you need such an accuracy, why is it important ? In most of the cases, you won't need that accuracy.Cédric Moonen Software developer
Charting control [v1.2] -
That's because atof returns a float (guess what the 'f' in the function name stands for ?). You could try by using stringstream from the STL:
stringstream ssVal;
double dValue;
ssVal << pszValue;
ssVal >> dValue;Don't forget to
#include <sstream>
and to putusing namespace std;
at the top of your cpp file. Forget what I say, I wasn't awake ;P . If you look at the range of a double, you'll see that the accuracy (here)[^] is 14 or 15 digits. Your string contains much more digits than that. Now, if the number that you want to represent is an integer value, why don't you use an __int64 instead ? But, I have a question: why do you need such an accuracy, why is it important ? In most of the cases, you won't need that accuracy.Cédric Moonen Software developer
Charting control [v1.2] -
Yeah, I realized that just after I posted. I updated my answer already.
Cédric Moonen Software developer
Charting control [v1.2]