Strange values when reading floats from a file

Started by
2 comments, last by peter_b 20 years ago
Hello. Im having a bit of a problem when it comes to reading floats form a text file for my serialization. The values seem to be modified a little by something, its really wierd. For example, i do:

out<<rotation<<std::endl;
where rotation is a float value of 180.0f. But later when i unserialize it:

instream>>rotation;
it will contain 179.000000061; or something like that, instead of 180. So you see the value is just a little off, which is so wierd. Now i know you should'nt use << and >> and use binary mode instead, but there is too much code to change it now. I have tried to write a little application doing nothing but reproducing the bug outside of my engine (which is huge). But i cant seem to reproduce it. Anyone encounterd this? Im running out hair to pull out... Thanks in advance. [edited by - peter_b on March 28, 2004 1:16:14 PM] [edited by - peter_b on March 28, 2004 1:17:01 PM] [edited by - peter_b on March 28, 2004 1:17:33 PM] [edited by - peter_b on March 28, 2004 1:18:15 PM]
Shields up! Rrrrred alert!
Advertisement
I''ve never encountered that before...but since you are using text mode can you just read it in as a string and then use atof() to convert it? Not a solution, but probably a fine workaround.

"That''s a very nice hat."
-Korben Dallas
"That's a very nice hat."-Korben Dallas
I'm going to guess that you're using DirectX. I'm also guessing that you're not creating your D3DDevice with D3DCREATE_FPU_PRESERVE. This causes the FPU to be put into single-precision mode, which can degrade the precision of these operations.

Read your compiler manual on how to toggle the FPU state during your IO code. Adding D3DCREATE_FPU_PRESERVE to your creation parameters would also work, but could potentially lower graphics performance.

"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on March 28, 2004 1:37:45 PM]
What is the value stored in the file?

Here is a possible scenario:

1. Your value is actually 179.99999998 due to floating-point imprecision.
2. When you write the number to the file, all the digits to the right of the decimal point are dropped for some reason, so the number is written as "179".
3. The value "179" is read as 179.00000061 due to floating-point imprecision.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement