I already know how floating point numbers stored in memory and I understand why expression 0.1 + 0.2 != 0.3 is True.
But I don't understand why 0.2f + 0.3f == 0.5f is true. Here is my code:
cout << setprecision(64)
<< "0.3 = " << 0.3 << "\n"
<< "0.2 = " << 0.2 << "\n"
<< "0.2 + 0.3 = " << 0.2 + 0.3 << "\n"
<< "0.3f = " << 0.3f << "\n"
<< "0.2f = " << 0.2f << "\n"
<< "0.2f + 0.3f = " << 0.2f + 0.3f << "\n";
I get output:
0.3 = 0.299999999999999988897769753748434595763683319091796875
0.2 = 0.200000000000000011102230246251565404236316680908203125
0.2 + 0.3 = 0.5
0.3f = 0.300000011920928955078125
0.2f = 0.20000000298023223876953125
0.2f + 0.3f = 0.5
I agree that if we sum 0.3 + 0.2 with double types a result will be 0.5, because 0.299999999999999988897769753748434595763683319091796875 + 0.200000000000000011102230246251565404236316680908203125 = 0.5.
But I still don't understand why sum 0.2f + 0.3f is 0.5 too. I expect the result will be 0.50000001490116119384765625 (0.300000011920928955078125 + 0.20000000298023223876953125). Could you please help me understand where I'm wrong?