# Big number question

• 01-10-2007
C of Green
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(;;)
{

if(inFile.eof() == true)
{
break;
}
if(byte == '*')
{
skipLine(inFile);
}
else if(byte == 'X')
{
inFile >> xcoord;
inFile >> ycoord;
inFile >> elevation;
}
}

...

any help would be great :)
• 01-10-2007
dwks
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

 Actually, Daved's right. A double should be able to represent 5606219.4 exactly. [/edit]
• 01-10-2007
Daved
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.
• 01-11-2007
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 :)
• 01-11-2007
hk_mp5kpdw
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.