Click to See Complete Forum and Search --> : wrong convert by atof function


zariostr
March 22nd, 2004, 10:55 AM
hello
i try to convert string type at double (items[] to d[])
but i get wrong results
for example

string was 6750.04799218269
double become 6750.9218269

string was -1.53354158708089E-7
double become -1.354158708089E-7

what's wrong ???


here is part of code, it's used atof function

int i;
int lbSize=0;
HWND preList,transformList;
string s;
int result[7];
string s1;
string items[7];
int len;
int j;
int pos;

double d[7];

preList=GetDlgItem(hDlg,IDC_LIST3);
transformList=GetDlgItem(hDlg,IDC_LIST1);
lbSize=SendMessage(preList,LB_GETCOUNT,0,0);
for (i=1;i<=lbSize;i=i+2)
{
SendMessage(preList,LB_GETTEXT,i,(LPARAM)&data);
s=data;
len=s.length();
result[0]=s.find(" ");
items[0]=s.substr(0,result[0]);

pos=items[0].find(",");
items[0].replace(pos,pos,".");
d[0]=atof(items[0].c_str());
...


thanks

TheCPUWizard
March 22nd, 2004, 11:01 AM
You seem to be replacing the wrong character in the string....

Print out the string after your replacements but before the atof call......

Sam Hobbs
March 22nd, 2004, 06:49 PM
Sometimes it helps to check assumptions. Also, it helps us to have sample code that is as simplified as possible that duplicates the problem. Perhaps if you had created a simpliifed sample you would realized that the problem is not what you assumed it is. The following is a sample simplifed sample.void show_atof(char *s) {
cout << s << " = " << atof(s) << '\n';
}

int main(int argc, char* argv[], char *envp[]) {
show_atof("6750.04799218269");
show_atof("-1.53354158708089E-7");
return 0;
}From that I get:

6750.04799218269 = 6750.05
-1.53354158708089E-7 = -1.53354e-007

TheCPUWizard
March 22nd, 2004, 07:09 PM
Sam, Look at the numbers...

string was 6750.04799218269
double become 6750.9218269

string was -1.53354158708089E-7
double become -1.354158708089E-7


Magically drop some digits aroung the decimal point (from the substition...


string was 6750./*0479*/9218269
double become 6750.9218269

string was -1./*53*/354158708089E-7
double become -1.354158708089E-7

/*I KNOW COMMENTS ARE NOT POSSIBLE IN A NUMBER!*/


Now notife the interesting fact that the number of digits I commented out is exactly equal to the number of digits to the left of the decimal point + the decimal point.

This simply indicates that there is a bug in his substitution!

Sam Hobbs
March 22nd, 2004, 08:26 PM
Originally posted by TheCPUWizard
This simply indicates that there is a bug in his substitution!I don't understand what you are saying. I truly do not.

I don't understand what the significance is of the "bug". If you are saying that there was a bug in zariostr's program then my post is consistent with that. My post shows how the problem could be diagnosed and how to help us help.

zariostr
March 23rd, 2004, 10:37 PM
i tested program with more details of debug
and really find that not right understand replace function
now i fixed it
thanks

zariostr
March 23rd, 2004, 10:41 PM
hi again
i make win32 application project
and want use some MFC classes
MFC and WinAPI using together is it possible ?
or not ?

Sam Hobbs
March 23rd, 2004, 11:11 PM
Yes, it is possible to use MFC and the WinAPI together, but it depends on how you use them together. In fact, the WinAPI is probably used in all MFC programs.

It depends on many things we don't know unless you tell us. However since this question is so different from the original question, there should be a new thread created in this forum to answer it.

Paul McKenzie
March 24th, 2004, 02:29 PM
Originally posted by zariostr
hi again
i make win32 application project
and want use some MFC classes
MFC and WinAPI using together is it possible ?
or not ? 1) Create a new thread for this question

2) Answers to this have already been given on CodeGuru. Do a search for full discussions on this topic.

3) In short, if the classes you want to use are the MFC container classes, you don't need MFC, just use the C++ standard classes.

Regards,

Paul McKenzie