0

Look at the following code. Why does the 0.01D constant make the output so inaccurate?

double x = 0D;
while (x < 2D) {
    System.out.println(x);
    x = x + 0.01D;
}

0.0
0.01
0.02
0.03
0.04
0.05
0.060000000000000005
0.07
0.08
0.09
0.09999999999999999
0.10999999999999999
0.11999999999999998
0.12999999999999998
0.13999999999999999
0.15

spongebob
  • 8,370
  • 15
  • 50
  • 83

0 Answers0