#include <iostream>
#include <iomanip>
int main()
{
float sum = 0.0f;
for (int idx = 0; idx < 100; ++idx) {
sum += 0.1f;
}
std::cout << std::fixed << std::setprecision(27) << sum << "\n";
constexpr float threshold = 0.00001f;
if ( std::abs( sum - 10.0f ) < threshold ) {
std::cout << "we have 10\n";
}
else {
std::cout << "we don't have a 10\n";
}
return 0;
}
/*
1 2 3
123456789012345678901234567890
9.999999999999980460074766597 -- double
10.000001907348632812500000000 -- float
0.1 decimal
1 2 3
123456789012345678901234567890
0 0111 1011 10011001100110011001101 binary
---------
exponent
1.10011001100110011001101
------------------------------------------------
Example: decimal 3.75
------------------------------------------------
binary fraction 11.11
normalized binary fraction 1.111 * 2^1 // exponent == 1
stored_e - 127 = 1
stored_e = 1 + 127 = 128 => 1000 0000
1 2 3
12345678901234567890123456789012
01000000011100000000000000000000
0.75 * 2 = 1.5
0.5 * 2 = 1.0
0.0 * 2 = 0.0 ....
------------------------------------------------
Example: decimal 0.1
------------------------------------------------
0.1 * 2 = 0.2
0.2 * 2 = 0.4
0.4 * 2 = 0.8
0.8 * 2 = 1.6
0.6 * 2 = 1.2
0.2 * 2 = 0.4
0.4 * 2 = 0.8
0.8 * 2 = 1.6
0.6 * 2 = 1.2
...
mantissa == 0001100110011001100110011001100....
0.0001100110011001100110011001100....
1.100110011001100110011001100.... * 2^-4
stored_e - 127 = -4
stored_e = 127 - 4 = 123 => 0111 1011
1 2 3
12345678901234567890123456789012
00111101110011001100110011001101
00111101110011001100110011001101
*/