Advertisement

Different formats of floating point value?

Started by June 01, 2001 11:55 AM
1 comment, last by stefu 23 years, 8 months ago
I''m trying to read data from a file to a struct, which includes. The problem is that it reads char- and int-values ok, but float values are something they shouldn''t. They should be betveen 0.0f and 1.0f but thay are 1473687514516.000430530400 or something! So is there different format for float-values. Changing byte orders or something?
1) If the data has come from a system with a different endian-ness, swap the bytes in the 32bits (your ints may be wrong too if this is the case though).


2) 4 sizes of IEEE floating point data 32, 64, 80 and 128 bits, make sure you have the right one.


3) Make sure you''re not reading it in as integer and casting to a float - it won''t work:

DWORD data;
read(&data);
float myfloat = (float)data;

won''t work - IEEE number is encoded into 3 parts (sign, mantissa and exponent [which is also offset] - all that would do is get the binary representation as an integer and then convert that integer to a float.


4) If its from a REALLY old system it might not be IEEE, you''ll need to know where the data is from in that case.

5) Could the data be in fixed point instead (particularly if it came from say a PlayStation or some other place without an FPU).

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Simon O'Connor | Technical Director (Newcastle) Lockwood Publishing | LinkedIn | Personal site

Advertisement
Thank you,

I just solved the problem. I made a massive byte by byte investigation and thouht that I had wrong specs because the struct was not matching. Actually the struct size was too big according to it''s member variables. It was DWORD aligned.

Adding the following lines solved the problem.
#pragma pack( push, 1 )
#pragma pack( pop )

Luckily I had faced it once before in DirectPlay network sample, but didn''t that time actually know what it meant!

Thank you anyway!

This topic is closed to new replies.

Advertisement