I tried the following for
loop:
for(double i = 0.0; i<=0.001; i+=0.0001)
System.out.println(i);
And I get the following output:
0.0
1.0E-4
2.0E-4
3.0000000000000003E-4
4.0E-4
5.0E-4
6.000000000000001E-4
7.000000000000001E-4
8.000000000000001E-4
9.000000000000002E-4
My questions are:
- How do these extra
.000000000000001
s come? - Will these extra numbers always come, or is there some problem in my code?
- Do these errors occur only in the Java language, or any other programming languages too?
- Is
double
suitable forfor
loops?