问题描述
对于 std::is_floating_point<T>::value
为 true
的类型 T
,C++ 标准是否在T
应该如何实现?
For types T
for which std::is_floating_point<T>::value
is true
, does the C++ standard specify anything on the way that T
should be implemented?
例如,T
是否必须遵循符号/尾数/指数表示?还是可以完全任意?
For example, does T
has even to follow a sign/mantissa/exponent representation? Or can it be completely arbitrary?
推荐答案
来自N3337:
[basic.fundamental/8]:
浮点类型共有三种:float、double 和 long double.double 类型至少提供与 float 一样多的精度,而 long double 类型提供的精度至少与 double 一样.float 类型的值集是 double 类型的值集的子集;一组值double 类型的值是 long double 类型的值集的子集.的价值表示浮点类型是实现定义的.整数和浮点类型统称为算术类型.标准模板 std::numeric_limits (18.3) 的特化应指定最大值以及实现的每种算术类型的最小值.
[basic.fundamental/8]:
There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.
如果你想检查你的实现是否使用 IEEE-754,你可以使用 std::numeric_limits::is_iec559
:
If you want to check if your implementation uses IEEE-754, you can use std::numeric_limits::is_iec559
:
static_assert(std::numeric_limits<double>::is_iec559,
"This code requires IEEE-754 doubles");
这方面还有许多其他的辅助特征,例如 has_infinity
, quiet_NaN
和 更多.
There are a number of other helper traits in this area, such as has_infinity
, quiet_NaN
and more.
这篇关于C++ 标准是否对浮点数的表示做了任何规定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!