Menu

#11 Java error writing little-endian floating point numbers

open
nobody
None
5
2008-11-18
2008-11-18
Philip Cook
No

The EndianCorrectOutputStream does not write little-endian floats correctly. The problem arises because the program does the following:

1. Convert the float to an int
2. Flip the int.
3. Convert the flipped int to a float.
4. Write the flipped float to disk.

Certain float values will map to NaN after flipping. For example, the value 57276.996 will be NaN after step 3. From the Java documentation for intBitsToFloat:

If the argument is 0x7f800000, the result is positive infinity.

If the argument is 0xff800000, the result is negative infinity.

If the argument is any value in the range 0x7f800001 through 0x7fffffff or in the range 0xff800001 through 0xffffffff

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Float.html#intBitsToFloat\(int)

Therefore, any float whose bits fall into one of the above ranges after flipping will be corrupted.

The flipped int should be written directly to disk, without converting back to a float value.

The same problem also applies to double.

Discussion


Log in to post a comment.