Thread: atof is not exact

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    8

    atof is not exact

    This is the code

    char number[]="0.58";
    double value = atof(number);

    When I execute it value is 0.57999999999999996

    Do you know why?

    Thanks in advance

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes. In computers, Floating points are only approximations.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Floats don't really store numbers the way you would probably think they do. They use a form of binary scientific notation (since your processor thinks in binary, not base 10).

    So for instance you may say "the road is 1.1*10^3 miles away" in scientific notation. Your computer may think in terms of "that river is 3.2*2^12 miles away."

    Converting between the two radii isn't accurate.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Isn't this in the FAQ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. atof function question
    By cjohnman in forum C Programming
    Replies: 4
    Last Post: 05-13-2008, 09:43 PM
  2. problem with atof
    By ssharish in forum C Programming
    Replies: 17
    Last Post: 03-24-2005, 07:53 AM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. atof()
    By Luigi in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2003, 10:08 AM
  5. how do atof, atoi, and atol work?
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 09:25 PM