What happens when l (long) - = f (float)?

public class SimplePrint { public static void main(String[] args) { long i = System.currentTimeMillis(); System.out.println(i); float h = 0.0f; i -= h; System.out.println(i); } } 

Output:

1477904636902

1477904695296

but when i changed the data type of the variable h

 public class SimplePrint { public static void main(String[] args) { long i = System.currentTimeMillis(); System.out.println(i); double h = 0.0f; i -= h; System.out.println(i); } } 

output changes:

1477904677513

1477904677513

Why is this???

+6
source share
2 answers

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 .

+6
source

Your code is basically:

 i = (long) (float) i; 

or

 i = (long) (double) i; 

Now in Java, a float has 23 bits of precision, while a double has 52 1477904677513 . 1477904677513 has 40 bits in it - 10101100000011001111110111011101010001001 , so when you convert to float, you lose the bottom 17 bits, so you see the values ​​change.

+5
source

Source: https://habr.com/ru/post/1011875/


All Articles