Public Group

# Read from file -0.000028 and get -2.8000000-e005 WHY!!!

This topic is 5468 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi. I am reading the following data from a text file: "0.000000, -0.000028, 0.912098" this way: if (sscanf(szLine, "%f, %f, %f", &pos.x, &pos.y, &pos.z ) == 3) and what is stored is: x = 0.000000 y = -2.8000000-e005 z= 0.91209799 So. Y is completely wrong and Z slightly wrong it should be 0.912098 in the end rather than 0.91209799. Why is that? It definetely something wrong with the floating point precission but how can I fix this?

##### Share on other sites
Y is not wrong, its mathematically correct if i remember my maths a-level.

-2.8 *10^-5 = -0.000028

as for the Z, make sure you are not confusing floats and doubles anywhere.

##### Share on other sites
First of all y is not wrong, it's just shifted by 5 digits (-e005)

The rest is slightly incorrect because of limited floating point precision, you could perhaps use doubles to get rid of that error.

##### Share on other sites
ok then so what do you do in order to store exactly same number into your data structure as in the text file. I am not using any doubles. All my program is based on floats. Well thats how I contructed my Vector class. Could someone please tell me how to fix this? I dont really wanna start playing with shifting stuff as some of them need that and some dont. There must be a simple way to do that. come on.
Maybve if I read them as doubles and cast to floats? or its even easier solution?

##### Share on other sites
As mentioned before, these two numbers are exactly the same, you don't need to do anything about it, e.g. you can write
y = -0.000028;

or
y = -2.8e-005;

in your program. You don't need to deal with shifting the numbers.

##### Share on other sites
Do you need your number to be exactly the same as they are in the text file? If so, the only way to do that is to use some custom type, likely a fixed point type, that you or someone else has to write.

I would suggest just working with floats, though, because you probably don't need any more accuracy. Besides, even with doubles, you'll still have inaccuracies; they'll just be smaller.

Oh, and read this (from the C++ FAQ Lite).

##### Share on other sites
That's the problem with floats. They only have a certain number of bits available to them, so precise floats out to a certain decimal place will be off a little.

Try displaying them with %g instead of %f in your printf.

##### Share on other sites
Use %5.5f, it will give you 5 decimals after the decimal point and at most 5 before (it won't padd with 0s like after the decimal point).

You can change the values to anything until it looks ok.

##### Share on other sites
Ok. Thank you all for help. Now I know why :)

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 16
• 13
• 14