Thread: stringstream to float problem

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    3

    stringstream to float problem

    When I try and convert a stringstream to a float it seems that I encounter a problem with float arithmetic when using the extraction operator. The line of code that causes problems is as follows:
    Code:
     ssConvert >> fValue
    from what i have seen it starts acting weird at 32.1 it will turn it into 32.100006 and then any float value after that.

    Thanks for any help.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You have to realize that floating point values are NOT PRECISE. It is very likely that you will get differences in the least significant digits of the number.

    So, given that, I don't expect stringstream to magically solve this issue, and you have to make sure that you understand the difference.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    from what i have seen it starts acting weird at 32.1 it will turn it into 32.100006 and then any float value after that.
    I cannot duplicate that for myself, but if it is just floating point inaccuracy, then there is little you can do about it as long as you want to use floating point.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could use a double and then this problem would happen less often than with floats.

  5. #5

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Does anyone then know of a way I could go from either a stringstream or a TCHAR array to a float value. I'm getting the value from a text box then converting into a float so I want to keep it as the value that is entered.

    Thanks for the help.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    As some mentioned already, if float produces weird results, try a double. If a double doesn't work, then you just have to accept some inaccuracies. Or you could get a library to give you better precision.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by c0d3m0nk3y View Post
    Does anyone then know of a way I could go from either a stringstream or a TCHAR array to a float value. I'm getting the value from a text box then converting into a float so I want to keep it as the value that is entered.

    Thanks for the help.
    If you want to keep it as the value entered, you'll need to keep it as a string.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    The double produces the same results as it is a value after a decimal point.

    EDIT:With a float the value becomes 32.099998 after coming out of the stringstream.

    It seems that after using the extration operator with the double the value becomes 32.100000000000001 when it should be 32.1 I'm not sure if that explains anything.

    Thanks for all the help.
    Last edited by c0d3m0nk3y; 01-31-2008 at 04:24 PM.

  10. #10
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Try to understand that there's an infinite amount of values - even between any two values. The floats and doubles only use 32 and 64 bits to store numbers, that is limited storage. That means that for most of the possible values the computer needs to use an approximation.

    In addition computers work in binary. In binary 32.1 is an infinite fraction the same way the result of 1/3, 5/7 or pi etc cannot be represented exactly in decimals.

    Anyway the difference between 32.1 and the value you are seeing is so tiny that you shouldn't be too much worried about it. If I'm not mistaken it would be the task of the output function to present it to the user rounded to some nice value again.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When producing the output from your application, you may want to restrict the precision of the number such that you only show, say, 5 decimal places. That way, 32.1 becomes 32.10000, which is close enough for most things - if that's not sufficiently precise, you will need to use a special library with extra precision.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem
    By ExDHaos in forum C++ Programming
    Replies: 12
    Last Post: 05-22-2009, 04:50 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  4. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM