I love FP; every time I think to had got it, I understand to know nothing about it :)
This is an example that I don't understand. I sum 8 time the same number (0.1) and I print the result, of both sum and "original":
std::cout.precision(100);
int numIteration = 8;
double step = 0.1;
double sum = 0.0;
for(int i = 0; i < numIteration; i++) {
sum += step;
}
std::cout << "orig stored as " << numIteration / 10.0 << std::endl;
std::cout << " sum stored as " << sum << std::endl;
0.1 is stored as 0.1000000000000000055511151231257827021181583404541015625, and I expect that after 8 sum, it will be stored bigger or equal of 0.8, which is stored as 0.8000000000000000444089209850062616169452667236328125.
But the result shock me. In fact after 8 sum, the result is 0.79999999999999993338661852249060757458209991455078125, which is smaller.
Also, if I check the binary output of both, I can see that the sum is "higher" than the "original":
0.8 stored as binary 0 01111111110 1001100110011001100110011001100110011001100110011001 // smaller
sum stored as binary 0 01111111110 1001100110011001100110011001100110011001100110011010 // higher
But 0.79999999999999993338661852249060757458209991455078125 < 0.8000000000000000444089209850062616169452667236328125.
Can you shine me?
EDIT: sorry to everybody, I had a mistake on copy/paste the binary. They were correct.