Thread: atof()

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    atof()

    I use atof() in a function but i get stranges values..

    when converted I get the following:
    3.4
    3.39999999999999991118215802999

    3.3
    3.29999999999999982236431605998

    3.2
    3.20000000000000017763568394002

    2.1
    2.10000000000000008881784197001

    Code:
    string temp_num = "";
    char dot = '.';
    while ((isdigit(temp_string[n])) || (temp_string[n] == dot))
    temp_num += temp_string[n++];
    number_value = atof(temp_num.c_str());
    what is wrong here? atof() itself?
    I also tried atol() but it wouldnt convert to decimal?

    I know someone else asked the same question but using a correction factor in my case wont solve my problem..
    Last edited by Luigi; 04-24-2003 at 11:47 PM.
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    "...but i get stranges values"

    I don't see any strange values. Where are they?

    "what is wrong here?"

    Where's here?

    "I also tried atol() but it wouldnt convert to decimal?"

    Maybe that's why they hit upon the function name "alpha to long"?

    You aren't going to get an exact representation of a base 10 number using base 2 which computers use. However, within about 10 decimal places, they are exact.
    Last edited by 7stud; 04-25-2003 at 02:21 AM.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Yep, floats aren't perfect. You are either going to have to find some way to deal with that, or use doubles.

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. how do atof, atoi, and atol work?
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 09:25 PM
  5. float to string reverse atof();
    By bigtamscot in forum C Programming
    Replies: 7
    Last Post: 01-30-2002, 04:58 PM