1

consider having a float number in java like this:

float f = 2000000000f;

After addition of number 64 the result is the same number:

System.out.println(String.format("%.0f", (f+64f)));
Result: 2000000000

After addition of number 65 the result is completely different:

System.out.println(String.format("%.0f", (f+65f)));
2000000128

can somebody explain to me why does this work like this? thanks

  • 1
    http://en.wikipedia.org/wiki/IEEE_floating_point -- observe how many decimal digits are supported for single-precision numbers. – Hot Licks May 13 '14 at 22:21
  • 1
    2000000000 is exactly representable as a float. 2000000128 is the smallest float greater than it, easily confirmed using `Math.nextUp`. 2000000064 is half way between, resulting in round to even, giving 2000000000. 2000000065 is closer to 2000000128, and so rounds to it. – Patricia Shanahan May 14 '14 at 08:51

0 Answers0