Here's the code:
typedef std::numeric_limits<float> fl;
int main()
{
std::cout.precision(100);
float f1 = 9999978e3;
std::cout << f1 << " (9999978e3 decimal number stored as binary)" << std::endl;
float f2 = 9999979e3;
std::cout << f2 << " (9999979e3 decimal number stored as binary)" << std::endl;
float f3 = 9999990e3;
std::cout << f3 << " (9999990e3 decimal number stored as binary)" << std::endl;
std::cout << "-----^" << std::endl;
}
which prints:
9999978496 (9999978e3 decimal number stored as binary)
9999978496 (9999979e3 decimal number stored as binary)
9999989760 (9999990e3 decimal number stored as binary)
-----^
The decimal numbers stored as binary correctly keep the 6th digit for 9999978e3
and 9999979e3
(which is 7), but the 6th digit of 9999990e3
is 8 and not 9.
Shouldn't floating point precision always guarantee the first 6 digits?
Yes, if I round 89 I got 9, but that's not the same; this will work/make sense only "visually".
Later (on processing numbers), when I'll apply math on that value, it'll work on a xxxxx8
number (9999989760), and not on a xxxxx9
. +1 at that magnitude.