As described in JLS Sec 15.26.2 , the compound assignment operator E1 op= E2
equivalent
E1 = (T) ((E1) op (E2))
where T is type E1.
So what do you do in the first case:
i = (long) (i - 0.0f)
To evaluate the value of -
, i
needs to be passed to the float
, as described in JLS Sec 15.18.2 :
Binary numeric promotion is performed on operands (Β§5.6.2).
and 5.6.2 :
Otherwise, if either operand is of type float, the other is converted to float.
The problem is that the value of i
cannot be represented exactly as a float
: because a float
has only 24 bits of value ( see here ), only values ββup to about 2 ^ 24 (= 16777216) can be represented; but the current time in milliseconds (at least on my machine) is about 1477905410000, which is much more.
So, you lose the precision of conversion to float, and this accuracy cannot be restored when returning to long
.
source share