Archived

This topic is now archived and is closed to further replies.

peter_b

Strange values when reading floats from a file

Recommended Posts

peter_b    200
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]

Share this post


Link to post
Share on other sites
GHoST    122
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

Share this post


Link to post
Share on other sites
Sneftel    1788
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]

Share this post


Link to post
Share on other sites
JohnBolton    1372
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.

Share this post


Link to post
Share on other sites