C++ 标准是否对浮点数的表示做了任何规定?

Does the C++ standard specify anything on the representation of floating point numbers?(C++ 标准是否对浮点数的表示做了任何规定?)
本文介绍了C++ 标准是否对浮点数的表示做了任何规定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 std::is_floating_point<T>::valuetrue 的类型 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++ 标准是否对浮点数的表示做了任何规定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)