0
System.out.print(0.7*0.7==0.49); 

Why does this code outputs false?

I saw this in my practice exam and I have am really confused.

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
Benjamin
  • 13
  • 2
  • 5
    Because of Floating-Point Arithmetic. Google it. – Maroun Dec 16 '14 at 10:34
  • How is numbers like 0.7 and 0.49 represented internally? Consider printing out 0.49 - 0.7*0.7 – Thorbjørn Ravn Andersen Dec 16 '14 at 10:34
  • Must-read: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – sp00m Dec 16 '14 at 10:35
  • because 0.7 is not actually 0.7 and neighter is 0.49 actually 0.49 internally – MihaiC Dec 16 '14 at 10:35
  • This would be a very peculiar exam that would require students to tell whether `0.7*0.7==0.49` is true or false in Java. The answer is completely determined by the IEEE 754 standard and the Java Language Specification's reliance on the former for floating-point, but converting `0.7` to binary64 by hand is not a useful skill to practice. – Pascal Cuoq Dec 16 '14 at 10:37

0 Answers0