What is the maximum number of base 10 digits in the integral part of a floating-point number

I want to know if there is something in the standard, for example, #define or something in numeric_limits that will tell me the maximum number of base 10 digits in the integral part of a floating point type.

For example, if I have a floating point type whose largest value is: 1234.567. I would like the standard to specify something specific that will tell me 4 for this type.

Can I do this?

 template <typename T> constexpr auto integral_digits10 = static_cast<int>(log10(numeric_limits<T>::max())) + 1; 
+3
source share
2 answers

The value you are looking for is max_exponent10 , which is:

Is the largest positive number n such that 10 n is a representable final floating point type value

Due to this relationship:

log 10 x = n
10 n = x

Your calculation does, finds how the first equation works:

 log10(numeric_limits<T>::max()) 

The definition of max_exponent10 explains that it uses 10 n + 1 will be greater than numeric_limits<T>::max() , but 10 n is less than or equal to numeric_limits<T>::max() . So, numeric_limits<T>::max_exponent10 is what you are looking for.

Please note that you still need + 1 , as in your example, to account for 1 place. (Because log 10 1 = 0) Thus, your number of ten-digit digits needed to represent numeric_limits<T>::max() will be:

 numeric_limits<T>::max_exponent10 + 1 

If it seems to you that you can verify this manually, follow these steps:

http://coliru.stacked-crooked.com/a/443e4d434cbcb2f6

+1
source

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 .

+1
source

Source: https://habr.com/ru/post/1257651/


All Articles