As Nathan Oliver notes in the comments, C ++ provides std::numeric_limits<T>::digits10 .
the number of base 10 digits that can be represented by type T without changes, that is, any number with this number of decimal digits can be converted to a value of type T and back to decimal form, without changes due to rounding or overflow. For base < radix types, this is the value of digits ( digits -1 for floating point types), multiplied by log 10 ( radix ) and rounded down.
The explanation for this is explained here by Rick Regan . Thus, if your binary floating-point format can store b bits in a value, then you are guaranteed to be able to round to ten-digit digits, where d is the largest integer such that
10 d 2 B-1
In the case of IEEE754 binary64 (the standard double in C ++ on most systems at present), then b = 53 and 2 b-1 = 4,503,599,627,370,496, so the format is guaranteed only for representing d = 15 digits.
However, this result holds for all numbers, while you are just asking about the integral part. However, we can easily find a counterexample by choosing x = 2 b +1, which is the smallest integer that is not representable in format: for binary, this is 9 007 199,254,740,993, which also has 16 digits, and therefore needs to be rounded .
source share