Thread: streams of double and precision

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

    streams of double and precision

    Hi,

    I have a peculiar problem when converting strings of numbers to double.

    I open files that contain numbers in lines, such as:
    Code:
    5.6 0 0.205867 1.0809 7.22644 0.373206 -5.84675e-317 1.99995 0.00433641 1.45331e-45
    I want to insert these numbers as double values in a vector of doubles.
    My problem is with very small values, lower than 1e-310.

    If I use this:
    Code:
    vector<double> vals;
    double x;
    while (stream >> x) {
          vals.push_back(x);
    }
    then the vector vals is truncated and contains: 5.6 0 0.205867 1.0809 7.22644 0.373206
    -5.84675e-317 is not only eliminated but the while loop ends.

    I get the same issue using STL algorithms:

    Code:
    copy(istream_iterator<double>(stream), istream_iterator<double>(), back_inserter(linevals) );
    However, this method works:
    Code:
    vector<double> vals;
    double x;
    string st;
    while (stream >> st) {
          x = atof(st.c_str());
          vals.push_back(x);
    }
    How come atof is able to convert very small values, but not the other methods?

    Thanks in advance.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What compiler you are using?
    I tried the following code in VS2005
    Code:
    #include <vector>
    #include <sstream>
    
    int main()
    {
    
    	std::stringstream stream("5.6 0 0.205867 1.0809 7.22644 0.373206 -5.84675e-317 1.99995 0.00433641 1.45331e-45");
    	std::vector<double> vals;
    	double x;
    	while (stream >> x) 
    	{
    		  vals.push_back(x);
    	}
    	return 0;
    }
    And it finished parsing the string without problems
    In debugger however I see for the value in question the following representation
    -5.846749631800e-317#DEN

    I do not know what does it mean
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    I think DEN means "denormalised" number: floating-point numbers follow a standard that changes their representation when they are close to 0.

    I use gcc/g++ 4.2.2 on Unix (IBM AIX). I have not tried under Linux though.
    I am going to try the code you show, thanks.
    Last edited by zeb; 02-27-2008 at 05:40 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Right, so a E-317 number is "invalid" according to your current "read double from stream" implementation. You would have to differentiate that from other errors in input. Not sure what the best choice for this would be - the problem comes when trying to tell the difference between a malformed number and one that is out of range. E.g. 3.12B is a malformed number, whilst 1.3E-317 is a out of range number. Not sure how you would go about doing that.

    Of course, you could read the number as a string and call strtod() to get it as a double, then check if ALL of the string was accepted by strtod().

    --
    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.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    3
    Thanks for the suggestions. I tried the code on Linux (same g++ version) and there is no problem. It seems to be a specific issue with denormalized numbers on AIX platform. I reported the observation to the GCC bugzilla, I'll see if this is confirmed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. numerical precision of a double
    By mc61 in forum C Programming
    Replies: 6
    Last Post: 03-11-2008, 12:23 PM
  2. extended vs double precision
    By Lind in forum C Programming
    Replies: 2
    Last Post: 09-19-2007, 02:02 PM
  3. More precision than a double
    By manutd in forum C++ Programming
    Replies: 6
    Last Post: 11-01-2006, 06:49 AM
  4. double precision and round-off errors
    By Micko in forum C Programming
    Replies: 2
    Last Post: 08-05-2004, 05:00 PM
  5. Replies: 15
    Last Post: 04-16-2003, 08:06 PM