0
  public static void main(String[] args)
  {
    System.out.println(0.9/3);
    System.out.println(0.6/3);
  }

  console result (complie with 1.8.0_45)
  0.3
  0.19999999999999998

Why I got 0.19999999999999998 instead of 0.2 like first statement 0.3? This question seems like ridiculous but I really want to know the reason.

Tawit K.
  • 41
  • 6
  • possible duplicate of [Is floating point math broken?](http://stackoverflow.com/q/588004/11683) – GSerg Aug 09 '15 at 08:20
  • A quite long explanation can be found here: http://www.exploringbinary.com/why-0-point-1-does-not-exist-in-floating-point/ – beosign Aug 09 '15 at 08:21
  • 1
    The answer is that `0.9` and `0.6` cannot be represented exactly so when you do a calculation on it, sometimes it can hide this representation error and sometimes it cannot. – Peter Lawrey Aug 09 '15 at 09:05

0 Answers0