Namespaces
Variants

std::numeric_limits<T>:: digits10

From cppreference.net
Utilities library
static const int digits10 ;
(C++11 이전)
static constexpr int digits10 ;
(C++11 이후)

std:: numeric_limits < T > :: digits10 의 값은 타입 T 로 변경 없이 표현될 수 있는 10진수 자릿수입니다. 즉, 이만큼의 유효 10진수 자릿수를 가진 어떤 숫자도 T 타입의 값으로 변환되었다가 다시 10진수 형태로 변환되어도, 반올림이나 오버플로로 인한 변경 없이 원래 값이 유지됩니다. radix 진법 타입의 경우, 이 값은 digits() (부동소수점 타입의 경우 digits - 1 )에 log 10 (radix) 를 곱한 후 내림한 값입니다.

표준 특수화

T std:: numeric_limits < T > :: digits10 의 값
/* 비특수화 */ 0
bool 0
char std:: numeric_limits < char > :: digits * std:: log10 ( 2 )
signed char std:: numeric_limits < signed char > :: digits * std:: log10 ( 2 )
unsigned char std:: numeric_limits < unsigned char > :: digits * std:: log10 ( 2 )
wchar_t std:: numeric_limits < wchar_t > :: digits * std:: log10 ( 2 )
char8_t (C++20부터) std:: numeric_limits < char8_t > :: digits * std:: log10 ( 2 )
char16_t (C++11부터) std:: numeric_limits < char16_t > :: digits * std:: log10 ( 2 )
char32_t (C++11부터) std:: numeric_limits < char32_t > :: digits * std:: log10 ( 2 )
short std:: numeric_limits < short > :: digits * std:: log10 ( 2 )
unsigned short std:: numeric_limits < unsigned short > :: digits * std:: log10 ( 2 )
int std:: numeric_limits < int > :: digits * std:: log10 ( 2 )
unsigned int std:: numeric_limits < unsigned int > :: digits * std:: log10 ( 2 )
long std:: numeric_limits < long > :: digits * std:: log10 ( 2 )
unsigned long std:: numeric_limits < unsigned long > :: digits * std:: log10 ( 2 )
long long (C++11 이후) std:: numeric_limits < long long > :: digits * std:: log10 ( 2 )
unsigned long long (C++11 이후) std:: numeric_limits < unsigned long long > :: digits * std:: log10 ( 2 )
float FLT_DIG ( 6 IEEE float 기준)
double DBL_DIG ( 15 IEEE double 기준)
long double LDBL_DIG ( 18 for 80-bit Intel long double ; 33 for IEEE quadruple)

예제

8비트 이진 타입은 두 자리 십진수를 정확히 표현할 수 있지만, 세 자리 십진수 256..999는 표현할 수 없습니다. 8비트 타입의 digits10 값은 2입니다 ( 8 * std:: log10 ( 2 ) 는 2.41입니다)

표준 32비트 IEEE 754 부동소수점 타입은 24비트의 분수부(23비트가 명시되고 하나는 암묵적)를 가지며, 이는 7자리 십진수를 표현할 수 있음을 시사할 수 있습니다( 24 * std:: log10 ( 2 ) 는 7.22). 그러나 상대 반올림 오차는 균일하지 않으며, 일부 7자리 십진수를 가진 부동소수점 값은 32비트 float로 변환했다가 다시 되돌리는 과정에서 유지되지 않습니다: 가장 작은 양의 예시는 8.589973e9 이며, 이는 왕복 변환 후 8.589974e9 가 됩니다. 이러한 반올림 오차는 표현에서 1비트를 초과할 수 없으며, digits10 ( 24 - 1 ) * std:: log10 ( 2 ) 로 계산되며, 이는 6.92입니다. 내림하면 값 6이 됩니다.

마찬가지로, 16자리 문자열 9007199254740993 는 텍스트->double->텍스트 왕복 변환에서 유지되지 못하고 9007199254740992 가 됩니다: 64비트 IEEE 754 타입 double은 15자리 십진수에 대해서만 이 왕복 변환을 보장합니다.

참고 항목

[static] (C++11)
이 타입의 모든 값을 구별하는 데 필요한 10진수 자릿수
(public static member constant)
[static]
주어진 타입의 표현에 사용되는 기수 또는 정수 베이스
(public static member constant)
[static]
변경 없이 표현할 수 있는 radix 자릿수
(public static member constant)
유효한 정규화된 부동소수점 값인 기수의 가장 작은 음의 거듭제곱보다 1 큰 값
(public static member constant)
유효한 유한 부동소수점 값인 기수의 가장 큰 정수 거듭제곱보다 1 큰 값
(public static member constant)