First, confirm that the conversion was correct.
Long.MAX_VALUE - 9223372036854775807 (19 digits). As you can see, the value roughly matches what you typed: 9.223372E18 .
The accuracy of a long always 1 . However, the accuracy of a float depends on the value of the number.
In IEEE, a single-precision floating-point number in which the float has only 24 bits of precision in the mantissa, or βfractionβ of the storage part. Thus, the actual value represented by the float is the approximate value of the actual value of Long.MAX_VALUE .
Float.floatToIntBits
As you understand, the Float.floatToIntBits method gives different bits than the original long representation.
Returns a representation of a given floating-point value according to the IEEE 754 floating-point floating-point mask. Bit 31 (the bit selected by mask 0x80000000) represents the sign of a floating-point number. The exponent metrics are bits 30-23 (bits selected by mask 0x7f800000). Bits 22-0 (bits that are selected by mask 0x007fffff) represent significant (sometimes called mantissa) floating point numbers.
(notch)
Returns : bits that represent a floating point number.
This method does not convert it to an int value, it only gives a bit representation of the float , which is stored in int . The value represented by this int is not expected to equal the float value.
Conversion
These zeros are the mantissa of the floating point number. The actual conversion of long to float involves finding the sign for the most significant bit, determining the value of the value to determine the exponent, and converting the rest of the value to the mantissa.
Since the precision of the float on the Long.MAX_VALUE scale Long.MAX_VALUE limited, some precision is lost. The end result is that the float value is slightly rounded. Since Long.MAX_VALUE 1 is less than power 2, rounding gives power 2, which is displayed as all zeros in the mantissa.
You can see the precision of the floating point value on a number scale with Math.ulp (the unit in last place).
Math.ulp(f)
what gives
1.09951163E12
As you can see, the difference is quite large for the float for Long.MAX_VALUE . (ULP for the corresponding double 2048.0 . Large, but much smaller than ulp for the float here.) But it matches the expected value of almost 10 19 - about 7 digits of precision for the float .