how do atof, atoi, and atol work?

This is a discussion on how do atof, atoi, and atol work? within the C++ Programming forums, part of the General Programming Boards category; how do atof, atoi, and atol work? I am trying to figure this out because I've noticed that sometimes when ...

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904

    how do atof, atoi, and atol work?

    how do atof, atoi, and atol work?

    I am trying to figure this out because I've noticed that sometimes when converting a string to a float, it messes up the number. For instance: I convert the string "33414132431.2" to a float (atof), and it turns the resulting value in to 33414132431.200001

    This is NOT a good thing when you are trying to compare two results together, and they aren't the same because they have a messed up end value.....

    I'm wondering if maybe it's just because I'm causing an overflow with a number that big.........well, just looking for some advise, no flaming.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    > "33414132431.2"

    there is no exact floating point representation for 0.2
    it is...
    // pseudo-binary
    0.0110 0110 0110 0110.... for ever and ever and ever.

    kinda like 3.14159......

    one way ive seen people account for this is to use a correction factor
    ie.
    Code:
    #define ICK 0.0001
    ....
    
    if(difference_in_numbers < ICK)
               close enough, must be equal
    else
             to far apart, not equal
    Last edited by Perspective; 04-24-2003 at 09:52 PM.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    aah, i see, so it's not the function's fault then. Ok, that should be fine then. thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM

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