May,
an important thing to remember when a long is converted to a float (widening conversion) is that there is no loss of magnitude but there may be a loss of precision due to the rounding of the least significant bits. Loosing some precision is regarded as less important than loosing orders of magnitude. That's why this conversion does not require any cast. What we want to keep is the magnitude of the long (reflected in the exponent of the float) and the maximal precision of the least significant bits (reflected in the significand of the float).
According to IEEE 754, a float is represented as the following bit
pattern:
0 00000000 00000000000000000000000
bit 0 : sign bit
bits 1-8 : exponent (stores the magnitude in powers of 2)
bits 9-31 : significand (stores the precision)
For instance, let's take
long big = 1234567890L;
float f = big;
System.out.println(big - (long)f);
Output: -46
Let's decompose the long and represent it as a float. The maximal magnitude (in powers of two) for "big" is 2^30 (1,073,741,824). Then let's take "big" and subtract the above magnitude and we are left with the remaining precision. (1,234,567,890 - 1,073,741,824) = 160,826,066
160,826,066 has to be stored in the 23 bits of the significand, but the binary representation of 160,826,066 is 1001 10010110 00000010 11010010 which takes 28 bits. Some least significant bits will be discarded but the overall magnitude (2^30) is kept.
[ August 07, 2002: Message edited by: Valentin Crettaz ]