0

Normally, we know that 0.5-0.4 is 0.1. But when I run the code, I get some wrong calculation or the result is not clear to me. Please, may you explain how compiler handle the floating point calculation in Java.

System.out.println(0.5-.4);   // my expected output 0.1
System.out.println(0.5f-.4f); //  my expected output 0.1

Output:

0.09999999999999998
0.099999994
  • Read this : http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html - What every computer scientist should know about floating point arithmetic –  Mar 01 '15 at 14:57
  • Why was that other comment deleted? I think it definitely is a duplicate of http://stackoverflow.com/questions/1661273/floating-point-arithmetic-not-producing-exact-results – KSFT Mar 01 '15 at 14:58
  • read this http://floating-point-gui.de/ and then the above oracle paper – phuclv Mar 01 '15 at 15:43

0 Answers0