CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: atof error

  1. #1
    Join Date
    Oct 2006
    Posts
    27

    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 ?

    --------------------------------------------------------------------------------

  2. #2
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

  3. #3
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    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.

  4. #4
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    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

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: atof error

    Quote 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.
    Quote 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.

  6. #6
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: atof error

    Quote 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

  7. #7
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: atof error

    Quote 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

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: atof error

    Quote 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.

  9. #9
    Join Date
    Dec 2005
    Location
    Prague, Czech Republic
    Posts
    208

    Re: atof error

    Quote 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

  10. #10
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    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

  11. #11
    Join Date
    Oct 2006
    Posts
    27

    Re: atof error

    ohh sorry for late reply....I tried with this header file...it worked...Thanks a lot

    Quote 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);
    
    }

  12. #12
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: atof error

    Quote 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()!

  13. #13
    Join Date
    Jun 2002
    Posts
    137

    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);
    }

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    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.

  15. #15
    Join Date
    Jun 2002
    Posts
    137

    Re: atof error

    Thanks! I searched, but didnot find the answer. Maybe not searching enough.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured