The key issue is that 0.1
can't be represented exactly in binary. Converting this to base 2, you get a number with a repetend in the fraction part, 1001
is repeating forever (just like in decimal, 1/3
ends up with 3
repeating forever).
When storing 0.1
in a float, it ends up getting rounded up in the last digit *), therefore the float
0.1 is larger than the double
0.1
So, yes, converting the float
to double
just appends (binary) zeros, but the float
was already a bit too large in the first place.
*) this actually depends on the representation of floating point numbers your implementation uses. With IEEE 754 float
s, the representation would end with
... 10011001100
and because the next digit would be a 1, rounding is done upwards, so the final result ends with
... 10011001101