I have read several topics about the display of floating point numbers display in C++ and I couldn't find a satisfying answer.
My question is: how to display all the significant digits of a floating point numbers in C++ in a scientific format (mantissa/exponent) ?
The problem is that all numbers do not have the same number of significant digits in base 10.
For example a double
has 15 to 17 significant decimal digits precision, but std::numeric_limits<double>::digits10
returns 15 and consequently, for some numbers I will loose 2 extra decimal digits of precision.
The value std::numeric_limits<double>::digits10
provides the number of decimal digits which can safely be restored, i.e., the number of decimal digits which survive a round-trip decimal->double
->decimal. Assuming more decimal digits are correct isn't helpful. If you want to faciliate the round-trip double
->decimal->double
you'd use std::numeric_limits<double>::max_digits10
.
If you want the exact values you'd use std::numeric_limits<double>::digits
but it will display numbers converted from decimal values in a funny way as they are normally rounded values. This is also the reason why max_digits10
isn't useful when presenting numbers for human consumption: the last couple of digits are normally not those expect by the human reader.