|
-
October 21st, 2006, 04:53 AM
#1
atof error
well what is wrong with this piece of code ?
#include <stdio.h>
main()
{
double input;
char mystring[50]="143.01";
printf ("%s \n",mystring);
input=atof(mystring);
printf("%f",input);
}
the last printf command is not printing the value 143.01
what is wrong over here ?
--------------------------------------------------------------------------------
-
October 21st, 2006, 05:47 AM
#2
Re: atof error
What is it printing then?
Take a look at this FAQ as well - C++ String: How to convert a string into a numeric type?
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 21st, 2006, 09:27 AM
#3
Re: atof error
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double input;
char mystring[50]="143.01";
printf ("%s \n",mystring);
input=atof(mystring);
printf("%f",input);
}
Last edited by JohnyDog; October 21st, 2006 at 09:30 AM.
-
October 21st, 2006, 10:05 AM
#4
Re: atof error
I suspect that sangfroid is not getting a double that ends in exactly .01. He's probably getting something like 143.0099999999999999999.
If so, then the answer is that even though double's are precise to 15 decimal digits, they are still only an approximation to the "real" number that your are trying to see.
There's no way around this. That's why doubles are usually avoided for financial transactions.
Mike
-
October 21st, 2006, 10:48 AM
#5
Re: atof error
 Originally Posted by MikeAThon
I suspect that sangfroid is not getting a double that ends in exactly .01. He's probably getting something like 143.0099999999999999999.
You are probably right.. but I wanted to know what the OP is getting because as mentioned by JohnyDog, he does not include stdlib and hence the code shouldn't compile even on a compliant compiler.
 Originally Posted by MikeAThon
There's no way around this. That's why doubles are usually avoided for financial transactions.
Just curios here, what kind of financial transactions are you referring to? I have worked on few applications dealing with derivatives pricing and we worked well with double.. although we were only looking for accuracy in not more than 5/6 decimal places. For the case here (OP), rounding rules should work fine.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 21st, 2006, 11:33 AM
#6
Re: atof error
 Originally Posted by exterminator
Just curios here, what kind of financial transactions are you referring to? I have worked on few applications dealing with derivatives pricing and we worked well with double.. although we were only looking for accuracy in not more than 5/6 decimal places. For the case here (OP), rounding rules should work fine.
I was being overly-simplisitc, and as usual, it got me in trouble.
It's usually impossible to avoid using doubles in financial calculations. For example, how would you ever be able to calcualte mortgages and the like?
I was referring to balance sheet-style situations, where it's necessary to add things up and check for equality with other summations. This can be done with doubles too (e.g., carry around an extra half-cent), but you need to be careful and you need to be aware of what you're doing.
Mike
-
October 21st, 2006, 12:11 PM
#7
Re: atof error
 Originally Posted by exterminator
You are probably right.. but I wanted to know what the OP is getting because as mentioned by JohnyDog, he does not include stdlib and hence the code shouldn't compile even on a compliant compiler.
Thats the thing - i actually tried the code (with gcc) before noticing the missing include and it compiled and run fine, just producing some random number in call to atof. I don't know if it is bug or name conflict with some gcc builtin function or what, but it is definitely interesting
-
October 21st, 2006, 12:19 PM
#8
Re: atof error
 Originally Posted by JohnyDog
Thats the thing - i actually tried the code (with gcc) before noticing the missing include and it compiled and run fine, just producing some random number in call to atof. I don't know if it is bug or name conflict with some gcc builtin function or what, but it is definitely interesting 
Which version? I compiled it on gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125). It complained of the missing header... and so did gcc version 3.4.2 (mingw-special) and comeau.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 21st, 2006, 12:32 PM
#9
Re: atof error
 Originally Posted by exterminator
Which version? I compiled it on gcc version 3.4.4 (cygming special, gdc 0.12, using dmd 0.125). It complained of the missing header... and so did gcc version 3.4.2 (mingw-special) and comeau.
4.1.0 - i got warning about undeclared function too, but it linked just fine
-
October 21st, 2006, 03:52 PM
#10
Re: atof error
this example should show whats going on.
Code:
#include <stdio.h>
/* #include <stdlib.h> fail to include header */
int main() {
double input, x;
double f = 143.01;
char mystring[50]="143.01";
printf ("s:%s\n",mystring);
printf ("f:%.2f\n",f);
input=atof(mystring); /* atof is not defined so an int return value is assumed */
/* that int is then converted to a double */
printf("%f\n",input);
x = *((int*)&f);
printf("%f\n",x); /* prints the same as above */
return 0;
}
BTW: gcc doesn't warn if the default warning level is used.
Kurt
-
October 22nd, 2006, 02:58 AM
#11
Re: atof error
ohh sorry for late reply....I tried with this header file...it worked...Thanks a lot
 Originally Posted by JohnyDog
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
double input;
char mystring[50]="143.01";
printf ("%s \n",mystring);
input=atof(mystring);
printf("%f",input);
}
-
October 22nd, 2006, 04:18 AM
#12
Re: atof error
 Originally Posted by exterminator
You are probably right.. but I wanted to know what the OP is getting because as mentioned by JohnyDog, he does not include stdlib and hence the code shouldn't compile even on a compliant compiler.
It typically does compile with a C89 compiler, though, behavior is undefined (and I hope you see it's pure C code).
IIRC, it may compile with a C++ compiler, because a standard header is allowed to include symbols from another standard header.
Now, the OP surely meets the problem of implicit definition of the symbol atof, as a function taking a char* parameter and returning int. This int is subsequently converted to a floating point number.
On x86 CPU, the convention for returning floating point numbers is different from the convention for int returns, and thus, ax/eax/rax contains a garbage int value after the call to atof.
What explains that the OP may get a value in range [-2147483648; 2147483647] with nothing except zeroes after the decimal point.
Well, actually, behavior is undefined by the standard.
In C99 and C++, implicit declarations have been banned.
"inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
Club of lovers of the C++ typecasts cute syntax: Only recorded member.
Out of memory happens! Handle it properly!
Say no to g_new()!
-
May 23rd, 2007, 08:28 PM
#13
Re: atof error
One question: I cannot tell why a float equal to 333.33 cannot be print correctly to screen? And after we run ff = f1 + f2; the result is not 333.33? does it mean that result of f1 + f2 will be stored as double then cast back to float for ff?
Thanks a lot in advance!
Code:
#include "stdio.h"
void printthefloat(float);
void printthedouble(double);
void main()
{
const char* str_float = "333.33";
double d;
float f1 = 333.0, f2 = 0.33, f3;
float ff = 333.33;
//try to print 333.0
//print 333.0
printthefloat(f1);
//try to print 0.33
//print 0.33
printthefloat(f2);
//try to print 333.33
//however print 333.329987
printthefloat(ff);
ff = f1 + f2;
if(ff == 333.33)
{
printf("we got ff equals to 333.33");
}
//try to print 333.33
//however print 333.329987
printthefloat(ff);
sscanf(str_float, "%lf", &d);
//try to print 333.33 as double
//print 333.33
printthedouble(d);
return;
}
void printthefloat(float f)
{
static int si = 0;
si += 1;
printf("input float-%d is now %f\n", si, f);
}
void printthedouble(double f)
{
static int si = 0;
si += 1;
printf("input double-%d is now %lf\n", si, f);
}
-
May 23rd, 2007, 08:39 PM
#14
Re: atof error
Please do a search here on CodeGuru. This has been discussed so many times here, and in great depth.
Simple answer -- floating point is not exact and cannot be represented exactly with a binary number system, unless the floating point number can be represented by sums of reciprocal powers of 2.
What is 0.33 in binary? Once you figure this out, then you have your answer. 0.33 in binary is a non-terminating, repeating, binary number.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; May 23rd, 2007 at 08:43 PM.
-
May 23rd, 2007, 09:50 PM
#15
Re: atof error
Thanks! I searched, but didnot find the answer. Maybe not searching enough.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|