atof is not exact

This is a discussion on atof is not exact within the C Programming forums, part of the General Programming Boards category; This is the code char number[]="0.58"; double value = atof(number); When I execute it value is 0.57999999999999996 Do you know ...

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

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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, 10: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, 11:08 AM
  5. how do atof, atoi, and atol work?
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 10:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21