Namespaces
Variants

std::numeric_limits<T>:: max_digits10

From cppreference.net
Utilities library
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)