C ++标准是否在浮点数的表示上指定了任何东西? [英] Does the C++ standard specify anything on the representation of floating point numbers?

查看:209
本文介绍了C ++标准是否在浮点数的表示上指定了任何东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 std :: is_floating_point< T> :: value 的类型 T true ,那么C ++标准是否指定了 T 应该被执行的方式?



例如, T 是否有符号/尾数/指数表示?或者它可以完全是任意的?

解决方案

从$ N3337:


[basic.fundamental / 8]:有三种浮点类型:float,double和long double。 double类型至少提供了
的精度,而float类型的精度至少与double相当。
float类型的值的集合是类型double值的集合的子集;类型double的值
的集合是类型long double的值集合的子集。
浮点类型的值表示是实现定义的
。积分和浮点类型统称为算术
类型。标准模板std :: numeric_limits(18.3)的专门化应指定实现的每个算术类型的最大值
和最小值。

如果你想检查你的实现是否使用IEEE-754,你可以使用 std :: numeric_limits :: is_iec559

  static_assert(std :: numeric_limits< double> :: is_iec559,
此代码需要IEEE-754双打);

这个区域还有一些其他的帮手特质,比如 has_infinity quiet_NaN 更多


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?

For example, does T has even to follow a sign/mantissa/exponent representation? Or can it be completely arbitrary?

解决方案

From 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.

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");

There are a number of other helper traits in this area, such as has_infinity, quiet_NaN and more.

这篇关于C ++标准是否在浮点数的表示上指定了任何东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆