Note that the representation of 0.1 in binary is infinite (periodic) thus it cannot be held in the limited memory of float. Of course, it could be represented as a fraction, but that's not how floats work.
I know that they are only approximations... but I thought that the calculation was "exact" and the display/internal saving of the numbers are approximations (machine accuracy)?
edit:
@hamsterman:
So even if I write the numbers exactly they are internally stored as approximations? That would explain the "easy" calculation going wrong. So every "noninteger" number is only an approximation, even the "easy" ones?
/edit
So every "noninteger" number is only an approximation, even the "easy" ones?
Not every, but the "easy" ones you think are easy, are different from the "easy" ones for the computer.
E.g. these are some example easy floats that are represented exactly:
Some integers can't be represented exactly by float. IINM, the float set doesn't contain any of the following:
* Odd numbers higher than 2^24.
* Non-divisors of 4 higher than 2^25.
* Non-divisors of 8 higher than 2^26.
Thank you everyone!
@Disch: The C++ FAQ was really great!
@rapidcoder: I have other calculations where mainly the numbers you mentioned are used. That was the reason for my question, since nearly all my calculations are exact (with 0.5 0.25... numbers) but some are not exact (with 0.1 1.4 ... numbers). Now I know I have to be careful with floating-point numbers.