c++ - C++ 标准是否对 float 的表示做了任何规定?

对于类型 T std::is_floating_point<T>::valuetrue , C++ 标准是否在 T 的方式上指定了任何内容?应该实现吗?

例如,T甚至遵循符号/尾数/指数表示?还是可以完全任意?

最佳答案

来自 N3337:

[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 :

static_assert(std::numeric_limits<double>::is_iec559,
              "This code requires IEEE-754 doubles");

这个区域还有许多其他的辅助特性,例如 has_infinity , quiet_NaN和 more .

https://stackoverflow.com/questions/34294938/

相关文章:

c++ - 析构函数可以是最终的吗?

c++ - "&s[0]"是否指向 std::string 中的连续字符?

c++ - 如何在 Windows 上找到 Qt5 CMake 模块

c++ - auto 和 decltype 的关系

c++ - 如何使用 C++ 模板减少编译时间

c++ - 三元表达式中的逗号混淆

c++ - vector 的 std::remove 和删除之间的区别?

c++ - 什么是 decltype 以及它是如何使用的?

c++ - 编译lua代码,存储字节码然后加载并执行

c++ - 与 win32 CRITICAL_SECTION 相比的 std::mutex 性能