1

I noticed an incomprehensible thing for me in static method round() in class Math:

Math.round(0.4999999999999999);  // is 0
Math.round(0.49999999999999999); // is 1

Why?

Gnоm
  • 13
  • 2

1 Answers1

14

0.49999999999999999 has too many significant digits and one double variable cannot store them all. So implicit rounding happens during compilation. By the time you are calling Math.round(), the argument is already 0.5 (check yourself: 0.49999999999999999 == 0.5 yields true).

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674