Yes, this is possible: if only for the reason that float
has too few (typically 6-7) significant digits to deal with all possible numbers that long
can represent (19 significant digits). This is in part due to the fact that float
has only 32 bits of storage, and long
has 64 (the other part is float's storage format † ). As per the JLS:
A widening conversion of an int or a long value to float, or of a long value to double, may result in loss of precision - that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).
By example:
long i = 1000000001; // 10 significant digits
float f = i;
System.out.printf(" %d %n %.1f", i, f);
This prints (with the difference highlighted):
1000000001
1000000000.0
~ ← lost the number 1
It is worth noting this is also the case with int
to float
and long
to double
(as per that quote). In fact the only integer → floating point conversion that won't lose precision is int
to double
.
~~~~~~
† I say in part as this is also true for int
widening to float
which can also lose precision, despite both int
and float
having 32-bits. The same sample above but with int i
has the same result as printed. This is unsurprising once you consider the way that float
is structured; it uses some of the 32-bits to store the mantissa, or significand, so cannot represent all integer numbers in the same range as that of int
.