Namespaces
Variants

std::numeric_limits<T>::max_digits10

cppreference.net에서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 사용 중단됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
스왑 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
 
static constexpr int max_digits10
(C++11 이후)

값 std::numeric_limits<T>::max_digits10은(는) 텍스트로의 직렬화/역직렬화에 필요한 것과 같이, 타입 T의 모든 서로 다른 값을 고유하게 표현하는 데 필요한 10진 자릿수입니다. 이 상수는 모든 부동소수점 타입에 대해 의미가 있습니다.

표준 특수화

T std:: numeric_limits < T > :: max_digits10 의 값
/* 비특수화 */ ​ 0 ​
bool ​ 0 ​
char ​ 0 ​
signed char ​ 0 ​
unsigned char ​ 0 ​
wchar_t ​ 0 ​
char8_t (C++20부터) ​ 0 ​
char16_t ​ 0 ​
char32_t ​ 0 ​
short ​ 0 ​
unsigned short ​ 0 ​
int ​ 0 ​
unsigned int ​ 0 ​
long ​ 0 ​
unsigned long ​ 0 ​
long long ​ 0 ​
unsigned long long ​ 0 ​
float FLT_DECIMAL_DIG 또는 std:: ceil ( std:: numeric_limits < float > :: digits * std:: log10 ( 2 ) + 1 )
double DBL_DECIMAL_DIG 또는 std:: ceil ( std:: numeric_limits < double > :: digits * std:: log10 ( 2 ) + 1 )
long double DECIMAL_DIG 또는 LDBL_DECIMAL_DIG 또는 std:: ceil ( std:: numeric_limits < long double > :: digits * std:: log10 ( 2 ) + 1 )

참고 사항

대부분의 수학 연산과 달리, 부동 소수점 값을 텍스트로 변환하고 다시 되돌리는 작업은 적어도 max_digits10 가 사용된 경우( 9 는 float 용, 17 는 double 용) 정확한 변환이 보장됩니다: 중간 텍스트 표현이 정확하지 않더라도 동일한 부동 소수점 값을 생성함이 보장됩니다. float 의 정확한 값을 10진 표기법으로 표현하려면 수백 자리의 10진수가 필요할 수 있습니다.

예제

#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
int main()
{
    float value = 10.0000086;
    constexpr auto digits10 = std::numeric_limits<decltype(value)>::digits10;
    constexpr auto max_digits10 = std::numeric_limits<decltype(value)>::max_digits10;
    constexpr auto submax_digits10 = max_digits10 - 1;
    std::cout << "float:\n"
                 "       digits10 is " << digits10 << " digits\n"
                 "   max_digits10 is " << max_digits10 << " digits\n"
                 "submax_digits10 is " << submax_digits10 << " digits\n\n";
    const auto original_precision = std::cout.precision();
    for (auto i = 0; i < 5; ++i)
    {
        std::cout
            << "   max_digits10: " << std::setprecision(max_digits10) << value << "\n"
               "submax_digits10: " << std::setprecision(submax_digits10) << value
            << "\n\n";
        value = std::nextafter(value, std::numeric_limits<decltype(value)>::max());
    }
    std::cout.precision(original_precision);
}

출력:

float:
       digits10 is 6 digits
   max_digits10 is 9 digits
submax_digits10 is 8 digits
   max_digits10: 10.0000086
submax_digits10: 10.000009
   max_digits10: 10.0000095
submax_digits10: 10.00001
   max_digits10: 10.0000105
submax_digits10: 10.00001
   max_digits10: 10.0000114
submax_digits10: 10.000011
   max_digits10: 10.0000124
submax_digits10: 10.000012

참고 항목

[static]
주어진 타입의 표현 방식에서 사용되는 기수 또는 정수 베이스
(public static member constant)
[static]
변경 없이 표현 가능한 radix 진법 자릿수의 개수
(public static member constant)
[static]
변경 없이 표현 가능한 10진수 자릿수의 개수
(public static member constant)
유효한 정규화된 부동소수점 값인 기수의 가장 작은 음의 거듭제곱보다 1 큰 값
(public static member constant)
유효한 유한 부동소수점 값인 기수의 가장 큰 정수 거듭제곱보다 1 큰 값
(public static member constant)