double x=0.2 is not exactly 1/5, it's exactly 0.200000000000000011102230246251565404236316680908203125
Thus, when you repeatedly add this number you cumulate small round off errors.
Assuming there is a strict application of IEEE 754 double precision operations (without intermediate extra precision), your loop would provide those double (I used the shortest decimal representation that would round to same double assuming correct rounding to nearest even):
-5.0 -4.8 -4.6 -4.3999999999999995 -4.199999999999999 -3.999999999999999 -3.799999999999999 -3.5999999999999988 -3.3999999999999986 -3.1999999999999984 -2.9999999999999982 -2.799999999999998 -2.599999999999998 -2.3999999999999977 -2.1999999999999975 -1.9999999999999976 -1.7999999999999976 -1.5999999999999976 -1.3999999999999977 -1.1999999999999977 -0.9999999999999978 -0.7999999999999978 -0.5999999999999979 -0.39999999999999786 -0.19999999999999785 2.1649348980190553e-15 0.20000000000000218 0.4000000000000022 0.6000000000000022 0.8000000000000023 1.0000000000000022 1.2000000000000022 1.4000000000000021 1.600000000000002 1.800000000000002 2.000000000000002 2.2000000000000024 2.4000000000000026 2.6000000000000028 2.800000000000003 3.000000000000003 3.2000000000000033 3.4000000000000035 3.6000000000000036 3.800000000000004 4.0000000000000036 4.200000000000004 4.400000000000004 4.600000000000004 4.800000000000004 5.000000000000004 5.200000000000005 5.400000000000005 5.600000000000005 5.800000000000005 6.000000000000005 6.2000000000000055 6.400000000000006 6.600000000000006 6.800000000000006 7.000000000000006 7.200000000000006 7.400000000000007 7.600000000000007 7.800000000000007 8.000000000000007 8.200000000000006 8.400000000000006 8.600000000000005 8.800000000000004 9.000000000000004 9.200000000000003 9.400000000000002 9.600000000000001 9.8 10.0
You can improve by using
for(int i=-25; i<=50; ++i) {
double x = i * 0.2;
But it's not perfect, 0.2 is not represented exactly as we saw above, and you get:
-5.0 -4.800000000000001 -4.6000000000000005 -4.4 -4.2 -4.0 -3.8000000000000003 -3.6 -3.4000000000000004 -3.2 -3.0 -2.8000000000000003 -2.6 -2.4000000000000004 -2.2 -2.0 -1.8 -1.6 -1.4000000000000001 -1.2000000000000002 -1.0 -0.8 -0.6000000000000001 -0.4 -0.2 0.0 0.2 0.4 0.6000000000000001 0.8 1.0 1.2000000000000002 1.4000000000000001 1.6 1.8 2.0 2.2 2.4000000000000004 2.6 2.8000000000000003 3.0 3.2 3.4000000000000004 3.6 3.8000000000000003 4.0 4.2 4.4 4.6000000000000005 4.800000000000001 5.0 5.2 5.4 5.6000000000000005 5.800000000000001 6.0 6.2 6.4 6.6000000000000005 6.800000000000001 7.0 7.2 7.4 7.6000000000000005 7.800000000000001 8.0 8.200000000000001 8.4 8.6 8.8 9.0 9.200000000000001 9.4 9.600000000000001 9.8 10.0
If you try with
for(int i=-25; i<=50; ++i) {
double x = i / 5.0;
It's the best that you can get with floating point, because you get the nearest floating point approximation to your multiples of 1/5:
-5.0 -4.8 -4.6 -4.4 -4.2 -4.0 -3.8 -3.6 -3.4 -3.2 -3.0 -2.8 -2.6 -2.4 -2.2 -2.0 -1.8 -1.6 -1.4 -1.2 -1.0 -0.8 -0.6 -0.4 -0.2 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0