std::numeric_limits<T>::round_style
From cppreference.com
static const std::float_round_style round_style; |
(C++11まで) | |
| static constexpr std::float_round_style round_style; |
(C++11以降) | |
T 型のオブジェクトに、T で正確に表現可能な値ではない値を格納する際に使用される丸めスタイルを識別します。
目次 |
[編集] 標準特殊化
T
|
std::numeric_limits<T>::round_style の値 |
| /* 非特殊化 */ | 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 から取得できます。
[編集] 例
10進数の値 0.1 は、2進浮動小数点型では表現できません。IEEE-754 double に格納される場合、 0x1.9999999999999*2-4 と 0x1.999999999999a*2-4 の間に位置します。最も近い表現可能な値への丸めは、 0x1.999999999999a*2-4 となります。
同様に、 0x1.3333333333333*2-2 と 0x1.3333333333334*2-2 の間にある10進数の値 0.3 は、最も近い値へ丸められ、 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) |