I'm reading this, but really I can't get why text-float-text
guarantee 6 digits, instead float-text-float
should 9 (considering single precision
).
Converting text-float-text
store into a float the correct precision. Only when printing occurs the "rounded" version. But so its a "printer" fault.
Code:
int main()
{
float decimalFloat = 8.589973e9;
char const *decimalString = "8.589973e9";
float const floatFromDecimalString = strtof(decimalString, nullptr);
std::cout << decimalString << std::endl << std::scientific << floatFromDecimalString << std::endl;
std::cout << "text-float-text: 6 digit preserved, not 7" << std::endl << std::endl;
std::cout << "but the value is correctly converted..." << std::endl;
std::cout << std::bitset<sizeof decimalFloat*8>(*(long unsigned int*)(&decimalFloat)) << std::endl;
std::cout << std::bitset<sizeof floatFromDecimalString*8>(*(long unsigned int*)(&floatFromDecimalString)) << std::endl;
}
The binary is preserved. Its equal between declaring floor directly or after the conversion from the same decimal stored as string:
01010000000000000000000000100110
Why we need digits10
? The number of preserved digits is max_digits10
. If print rounds "badly", well... it seems a problem of the printer.
One should know that the actual float value significant digits are max_digits10
and not digits10
(even if you are looking at digits10 once printed).