std::numeric_limits<T>::round_style
来自cppreference.com
< cpp | types | numeric limits
| 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 的值表示的值存储于该类型的对象时所用的舍入模式。
标准特化
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 | std::round_toward_zero |
| char16_t | std::round_toward_zero |
| char32_t | 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 | std::round_toward_zero |
| unsigned long long | 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> 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' << "The rounding style is " << 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 The rounding style is 1
参阅
| 指示浮点舍入模式 (枚举) |