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.
precision(std::numeric_limits<double>::digits10 + 2)
? Of course, if the last two digits are "random garbage generated via your calculations (e.g.1.00000000000005 + 100 - 100
[assuming compiler didn't remove the +100 - 100 as meaningless, for example because those are variables unknown at the time]). It should be noted that for example glibc's printf will produce "garbage" if you print something likedouble value = 1.234E+300
asprintf("%f", value);
- there will be about 280 digits of 'randomness') – Buddybuderuscout
does in this case. – Buddybuderusdigits10
. Those digits are the ones that might not be preserved in a conversion from decimal to floating-point in back, and they are only the digits affected by the smallest possible rounding in floating-point. Once one performs more than a single calculation, the errors may compound in a variety of ways, yielding a compounded error that can range from zero to infinity, depending on circumstances. – Aftergrowthdigits10
. Therefore, you must have more digits. – Aftergrowth