Thread: Big number question

  1. #1
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59

    Big number question

    Hi all, just wondering if someone can tell me what i might be doing wrong here ...

    I'm trying to read in numbers from a text file into variables and display them back on the screen. However after i read them and output them, they are not the same. Here is what the input would looklike

    Code:
    ****************************************
    ****************************************
    ****************************************
    X 362741.5      5606219.4          896.6 
    X 362731.9      5606201.4          896.2 
    X 362722.8      5606184.1          895.8
    and here is the output

    Code:
    362742        5.60622e+006       896.6
    362732        5.6062e+006        896.2
    362723        5.60618e+006       895.8
    so only the last set of numbers stays the same ?

    here is some of the code im using ...

    Code:
    	...
    
    	char byte;
    	double xcoord;
    	double ycoord;
    	double elevation;
    
    	for(;;)
    	{
    		inFile.read(&byte, 1);
    
    		if(inFile.eof() == true)
    		{
    			break;
    		}
    		if(byte == '*')
    		{
    			skipLine(inFile);
    		}
    		else if(byte == 'X')
    		{
    			inFile >> xcoord;
    			inFile >> ycoord;
    			inFile >> elevation;
    		}
    	}
    
    	...
    any help would be great

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You've run into floating point inaccuracies. Not all numbers can be represented exactly.

    If you don't want numbers to be printed in scientific notation, you can turn it off. (Don't ask me how. )

    If you want a long read, look at this: http://en.wikipedia.org/wiki/Floating_point

    [edit] Actually, Daved's right. A double should be able to represent 5606219.4 exactly. [/edit]
    Last edited by dwks; 01-10-2007 at 05:02 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    If you use <iomanip> to set the correct notation they should come out the way they were input. The inaccuracy of floating point numbers shouldn't stop you from being able to output these numbers the way they looked originally.

  4. #4
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    hmmm ... ya i tried playing around with <iomanip> still could not get it to work.

    Questions ... Why does the first number get lose the decimal and get rounded up? why does the second number turn into scientific? Why does the last number stay the same and retain the decimal?

    oh wait ... the setprecision worked ... thanks
    Last edited by C of Green; 01-11-2007 at 11:58 AM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by C of Green
    hmmm ... ya i tried playing around with <iomanip> still could not get it to work.

    Questions ... Why does the first number get lose the decimal and get rounded up? why does the second number turn into scientific? Why does the last number stay the same and retain the decimal?

    oh wait ... the setprecision worked ... thanks
    The default precision is 6 places I believe; that's why values such as 362741.5 and 5606219.4 are displayed as 362742 and 5.60622e+006 (both 6 places). 896.6 on the other hand is already less than 6 places and so it displays just as it is.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flipping number question..
    By transgalactic2 in forum C Programming
    Replies: 4
    Last Post: 12-09-2008, 09:02 AM
  2. Question about limiting the number of user inputs in C
    By chobibo in forum C Programming
    Replies: 15
    Last Post: 08-30-2006, 12:37 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. indefinite number of datapoints question
    By Wick in forum C++ Programming
    Replies: 1
    Last Post: 09-04-2003, 09:33 AM
  5. Replies: 1
    Last Post: 07-01-2002, 05:36 PM