Namespaces
Variants

std::numeric_limits<T>:: round_style

From cppreference.net
Utilities library
static const std:: float_round_style round_style ;
(C++11 이전)
static constexpr std:: float_round_style round_style ;
(C++11 이후)

std:: numeric_limits < T > :: round_style 값은 부동소수점 타입 T 가 자신의 정확하게 표현 가능한 값 중 하나가 아닌 값을 해당 타입의 객체에 저장할 때 사용되는 반올림 방식을 식별합니다.

목차

표준 특수화

T std:: numeric_limits < T > :: round_style 의 값
/* non-specialized */ std::round_toward_zero
bool std::round_toward_zero
char std::round_toward_zero
signed char std::round_toward_zero
unsigned char std::round_toward_zero
wchar_t std::round_toward_zero
char8_t (C++20부터) std::round_toward_zero
char16_t (C++11부터) std::round_toward_zero
char32_t (C++11부터) std::round_toward_zero
short std::round_toward_zero
unsigned short std::round_toward_zero
int std::round_toward_zero
unsigned int std::round_toward_zero
long std::round_toward_zero
unsigned long std::round_toward_zero
long long (C++11부터) std::round_toward_zero
unsigned long long (C++11부터) std::round_toward_zero
float 일반적으로 std::round_to_nearest
double 일반적으로 std::round_to_nearest
long double 일반적으로 std::round_to_nearest

참고 사항

이 값들은 상수이며, std::fesetround 에 의해 변경된 반올림 설정을 반영하지 않습니다. 변경된 값은 FLT_ROUNDS 또는 std::fegetround 에서 얻을 수 있습니다.

예제

십진수 값 0.1 는 이진 부동 소수점 타입으로 정확히 표현될 수 없습니다. IEEE-754 double 에 저장될 때, 이 값은 0x1.9999999999999*2 -4
0x1.999999999999a*2 -4
사이에 위치합니다. 가장 가까운 표현 가능한 값으로 반올림하면 0x1.999999999999a*2 -4
가 됩니다.

마찬가지로, 소수 값 0.3 0x1.3333333333333*2 -2
0x1.3333333333334*2 -2
사이에 위치하며, 최근접 반올림되어 0x1.3333333333333*2 -2
로 저장됩니다.

#include <iostream>
#include <limits>
auto print(std::float_round_style frs)
{
    switch (frs)
    {
        case std::round_indeterminate:
            return "Rounding style cannot be determined";
        case std::round_toward_zero:
            return "Rounding toward zero";
        case std::round_to_nearest:
            return "Rounding toward nearest representable value";
        case std::round_toward_infinity:
            return "Rounding toward positive infinity";
        case std::round_toward_neg_infinity:
            return "Rounding toward negative infinity";
    }
    return "unknown round style";
}
int main()
{
    std::cout << std::hexfloat
              << "The decimal 0.1 is stored in a double as "
              << 0.1 << '\n'
              << "The decimal 0.3 is stored in a double as "
              << 0.3 << '\n'
              << print(std::numeric_limits<double>::round_style) << '\n';
}

출력:

The decimal 0.1 is stored in a double as 0x1.999999999999ap-4
The decimal 0.3 is stored in a double as 0x1.3333333333333p-2
Rounding toward nearest representable value

참고 항목

부동소수점 반올림 모드를 나타냄
(enum)