Thread: how do atof, atoi, and atol work?

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905

    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 08:52 PM.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    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