posted 11 years ago

We need to convert Float to Binary and we are using the following APIs:

1) Float.floatToIntBits(<float>

2) Integer.toBinaryString(<intbits>

The usage is:

String <string> = Integer.toBinaryString(Float.floatToIntBits(<float>

For most cases we get the correct Binary String. However, when the float variable has value = 8 digit integers, we run into the following problem:

Problem: floatToIntBits Returns the SAME output for different Input values.

Example:

public class TestFloatToBinary

{

//Function to convert the float value to binary

public static void main(String args[])

{

float f1 = 17012219;

float f2 = 17012220;

float f3 = 17012221;

float f4 = 17012222;

String sBinary1 = Integer.toBinaryString(Float.floatToIntBits(f1));

String sBinary2 = Integer.toBinaryString(Float.floatToIntBits(f2));

String sBinary3 = Integer.toBinaryString(Float.floatToIntBits(f3));

String sBinary4 = Integer.toBinaryString(Float.floatToIntBits(f4));

System.out.println("float to intbits for f1 = " + Float.floatToIntBits(f1));

System.out.println("float to intbits for f2 = " + Float.floatToIntBits(f2));

System.out.println("float to intbits for f3 = " + Float.floatToIntBits(f3));

System.out.println("float to intbits for f4 = " + Float.floatToIntBits(f4));

System.out.println("Binary for f1 = " + sBinary1);

System.out.println("Binary for f2 = " + sBinary2);

System.out.println("Binary for f3 = " + sBinary3);

System.out.println("Binary for f4 = " + sBinary4);

}

}

-----------------------------------------------

Output:

C:\work\krishna\java>java TestFloatToBinary

float to intbits for f1 = 1266797310

float to intbits for f2 = 1266797310

float to intbits for f3 = 1266797310

float to intbits for f4 = 1266797311

Binary for f1 = 1001011100000011100101011111110

Binary for f2 = 1001011100000011100101011111110

Binary for f3 = 1001011100000011100101011111110

Binary for f4 = 1001011100000011100101011111111

As seen above, floatToIntBits and hence Integer.toBinaryString return the SAME value for Inputs 17012219, 17012220, 17012221.

What is the reason why this happens?

Are we missing something here??

Appreciate your help!

Thank you.

1) Float.floatToIntBits(<float>

2) Integer.toBinaryString(<intbits>

The usage is:

String <string> = Integer.toBinaryString(Float.floatToIntBits(<float>

For most cases we get the correct Binary String. However, when the float variable has value = 8 digit integers, we run into the following problem:

Problem: floatToIntBits Returns the SAME output for different Input values.

Example:

public class TestFloatToBinary

{

//Function to convert the float value to binary

public static void main(String args[])

{

float f1 = 17012219;

float f2 = 17012220;

float f3 = 17012221;

float f4 = 17012222;

String sBinary1 = Integer.toBinaryString(Float.floatToIntBits(f1));

String sBinary2 = Integer.toBinaryString(Float.floatToIntBits(f2));

String sBinary3 = Integer.toBinaryString(Float.floatToIntBits(f3));

String sBinary4 = Integer.toBinaryString(Float.floatToIntBits(f4));

System.out.println("float to intbits for f1 = " + Float.floatToIntBits(f1));

System.out.println("float to intbits for f2 = " + Float.floatToIntBits(f2));

System.out.println("float to intbits for f3 = " + Float.floatToIntBits(f3));

System.out.println("float to intbits for f4 = " + Float.floatToIntBits(f4));

System.out.println("Binary for f1 = " + sBinary1);

System.out.println("Binary for f2 = " + sBinary2);

System.out.println("Binary for f3 = " + sBinary3);

System.out.println("Binary for f4 = " + sBinary4);

}

}

-----------------------------------------------

Output:

C:\work\krishna\java>java TestFloatToBinary

float to intbits for f1 = 1266797310

float to intbits for f2 = 1266797310

float to intbits for f3 = 1266797310

float to intbits for f4 = 1266797311

Binary for f1 = 1001011100000011100101011111110

Binary for f2 = 1001011100000011100101011111110

Binary for f3 = 1001011100000011100101011111110

Binary for f4 = 1001011100000011100101011111111

As seen above, floatToIntBits and hence Integer.toBinaryString return the SAME value for Inputs 17012219, 17012220, 17012221.

What is the reason why this happens?

Are we missing something here??

Appreciate your help!

Thank you.

posted 11 years ago

You need to understand that floating point data types such as

You cannot store any number with an arbitrary number of digits and expect that it is stored with infinite precision.

The precision of a 32-bit IEEE 754 floating point number such as

IEEE Standard 754 Floating Point Numbers

IEEE floating-point standard (Wikipedia)

[ July 26, 2006: Message edited by: Jesper Young ]

**float**and**double**do not have infinite precision.You cannot store any number with an arbitrary number of digits and expect that it is stored with infinite precision.

The precision of a 32-bit IEEE 754 floating point number such as

**float**in Java is about 5 or 6 decimal digits, so it's no surprise that numbers that differ beyond about the 5th digit only look the same in binary form.IEEE Standard 754 Floating Point Numbers

IEEE floating-point standard (Wikipedia)

[ July 26, 2006: Message edited by: Jesper Young ]

posted 11 years ago

Apart from the precision problem, there is something else not right here. You start with positive numbers, and end up with a String starting with a 1.

If you count the bits printed, you find they only come to 32 if you use a negative number. So your "toBinaryString" is losing its leading zeroes.

If you count the bits printed, you find they only come to 32 if you use a negative number. So your "toBinaryString" is losing its leading zeroes.

posted 11 years ago

Please don't cross-post. I deleted the copy of this thread in the Advanced forum.