Problem with atof() in Glibc? - Linux

This is a discussion on Problem with atof() in Glibc? - Linux ; When I compile and run this code: #include int main() { char *a = "0.3"; double d = atof(a); printf("Result is %f\n", d); return(0); } the result is 858993459.000000 Is there a bug in the implementation of atof() in Glibc? ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Problem with atof() in Glibc?

  1. Problem with atof() in Glibc?


    When I compile and run this code:

    #include

    int main()
    {

    char *a = "0.3";

    double d = atof(a);

    printf("Result is %f\n", d);

    return(0);
    }

    the result is 858993459.000000

    Is there a bug in the implementation of atof() in Glibc?
    atoi() works fine (with integers ofcourse).

    Does anybody has a clue?


  2. Re: Problem with atof() in Glibc?

    teuniz@gmail.com wrote:
    > When I compile and run this code:
    >
    > #include
    >
    > int main()
    > {
    >
    > char *a = "0.3";
    >
    > double d = atof(a);
    >
    > printf("Result is %f\n", d);
    >
    > return(0);
    > }
    >
    > the result is 858993459.000000
    >
    > Is there a bug in the implementation of atof() in Glibc?
    > atoi() works fine (with integers ofcourse).
    >
    > Does anybody has a clue?


    I hope I do ;-)

    How should the C compiler know what atof() returns?

    "man atof" here says to include stdlib.h!

    As it stands, the C compiler assumes atof() will return an integer and
    will then convert that integer to a double.

    Josef
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html


  3. Re: Problem with atof() in Glibc?

    Josef Moellers wrote:

    > teuniz@gmail.com wrote:
    >
    >> When I compile and run this code:
    >>
    >> #include
    >>
    >> int main()
    >> {
    >>
    >> char *a = "0.3";
    >>
    >> double d = atof(a);
    >>
    >> printf("Result is %f\n", d);
    >>
    >> return(0);
    >> }
    >>
    >> the result is 858993459.000000
    >>
    >> Is there a bug in the implementation of atof() in Glibc?
    >> atoi() works fine (with integers ofcourse).
    >>
    >> Does anybody has a clue?

    >
    >
    > I hope I do ;-)
    >
    > How should the C compiler know what atof() returns?
    >
    > "man atof" here says to include stdlib.h!
    >
    > As it stands, the C compiler assumes atof() will return an integer and
    > will then convert that integer to a double.
    >
    > Josef


    I also suggest to use sscanf instead of atof, since with atof you can't
    distinguish between "0.0" and an unvalid input such as "aosdsod".

    --
    SF

    "Spartani... mangiate tanto a colazione perchè stasera ceneremo nell'Ade!!"

  4. Re: Problem with atof() in Glibc?


    >"man atof" here says to include stdlib.h!


    You are right, this is the solution.
    The weird thing is that I didn't get a compilerwarning about using
    atof() without defining it in stdlib.h.
    Strange?

    Anyway, tnx for your quick response.

    Grtz, Teuniz



  5. Re: Problem with atof() in Glibc?

    teuniz@gmail.com wrote:

    >>"man atof" here says to include stdlib.h!

    >
    >
    > You are right, this is the solution.
    > The weird thing is that I didn't get a compilerwarning about using
    > atof() without defining it in stdlib.h.
    > Strange?


    if a C compiler finds a function that was not precedently declared, it assumes
    its declaration was

    int function();

    anyway, it's strange you aren't seeing any warnings. Assuming you are using
    gcc, try yo pass -Wall as compiling option.

    --
    SF

    "Spartani... mangiate tanto a colazione perchè stasera ceneremo nell'Ade!!"

  6. Re: Problem with atof() in Glibc?

    In article <1181910171.754736.61800@n2g2000hse.googlegroups.co m>,
    wrote:
    >
    >When I compile and run this code:
    >
    >#include
    >
    >int main()
    >{
    >
    > char *a = "0.3";
    >
    > double d = atof(a);
    >
    > printf("Result is %f\n", d);
    >
    > return(0);
    >}
    >
    >the result is 858993459.000000
    >
    >Is there a bug in the implementation of atof() in Glibc?
    >atoi() works fine (with integers ofcourse).
    >
    >Does anybody has a clue?


    gcc -Wall says:

    warning: implicit declaration of function `atof'

    atof is declared in

    atoi() works fine because function-type defaults to integer.

    regards

    winfried

    --
    Winfried Magerl - Internet Administration
    Siemens IT Solutions and Services, 81739 Munich, Germany
    Internet-Mail: winfried.magerl@siemens.com

  7. Re: Problem with atof() in Glibc?

    Dildo Baggins writes:

    > Josef Moellers wrote:
    >
    >> teuniz@gmail.com wrote:
    >>
    >>> When I compile and run this code:
    >>>
    >>> #include
    >>>
    >>> int main()
    >>> {
    >>>
    >>> char *a = "0.3";
    >>>
    >>> double d = atof(a);
    >>>
    >>> printf("Result is %f\n", d);
    >>>
    >>> return(0);
    >>> }
    >>>
    >>> the result is 858993459.000000
    >>>
    >>> Is there a bug in the implementation of atof() in Glibc?
    >>> atoi() works fine (with integers ofcourse).
    >>>
    >>> Does anybody has a clue?

    >> I hope I do ;-)
    >> How should the C compiler know what atof() returns?
    >> "man atof" here says to include stdlib.h!
    >> As it stands, the C compiler assumes atof() will return an integer
    >> and will then convert that integer to a double.
    >> Josef

    >
    > I also suggest to use sscanf instead of atof, since with atof you
    > can't distinguish between "0.0" and an unvalid input such as "aosdsod".


    That's a valid point, but sscanf is overkill in this case. Using
    strtod() will also do the trick.

    --
    Måns Rullgård
    mans@mansr.com

  8. Re: Problem with atof() in Glibc?

    In article <1181910171.754736.61800@n2g2000hse.googlegroups.co m>,
    wrote:

    >When I compile and run this code:
    >
    >#include


    Have you considered include the .h file that actually
    defines atof? I think that'd help quite a bit.

    --
    http://www.spinics.net/lists/

    --

+ Reply to Thread