C ++ 03保证的double是否正好表示小整数? [英] Is `double` guaranteed by C++03 to represent small integers exactly?

查看:151
本文介绍了C ++ 03保证的double是否正好表示小整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 03标准是否保证足够小的非零整数在 double 中正确表示?如果不是,C ++ 11怎么样?注意,我不是在这里假设IEEE合规。



我怀疑答案是,但我很想被证明是错误的。 / p>

当我说足够小时,我的意思是,由一些可以从C ++ 03的保证导出的值,可以通过 std :: numeric_limits< double> 提供的值计算。





$ b b

编辑:



很清楚(现在我已经检查过了) std :: numeric_limits< double& code>与 DBL_MANT_DIG 相同, std :: numeric_limits< double> :: digits10 DBL_DIG 相同的事情,这对于C ++ 03和C ++ 11都是如此。



此外,C ++ 03延伸到C90,并且C ++ 11相对于 DBL_MANT_DIG DBL_DIG



C90和C99都规定 DBL_DIG 的最小允许值为10, 10个十进制数字。



问题是,这是什么意思?是否意味着最多10位十进制数的整数保证在 double



case,那么C99中 DECIMAL_DIG 的目的是什么,以及C99中的以下注释§5.2.4.2.2/ 12?


使用DECIMAL_DIG数字和后面的
从(至少)double转换为decimal应为标识函数。







这里是C99§5.2.4.2.2/ 9对 DBL_DIG

 十进制数字,'q',以便任何浮点数
数字与'q'十进制数字可以舍入为带有'p'个基数'b'数字的
浮点数,并返回
而不改变为q个十进制数字:

{p * log10 (b)如果'b'是10的幂10
{
{floor((p-1)* log10(b))否则

FLT_DIG 6
DBL_DIG 10
LDBL_DIG 10

如果有人能帮我解压这个,我会很高兴。

解决方案

很好,3.9.1 [basic.fundamental]第8段规定


...浮点类型的值表示是实现定义的。 ...


至少,实现必须定义它使用的表示形式。



另一方面, std :: numeric_limits< F> 定义了几个成员,这些成员似乎意味着该表示是某些形式的有效位基数指数




  • std :: numeric_limits& :: radix :指数的底数

  • std :: numeric_limtis< F> :: digits radix 数字



我认为这些语句意味着你可以完全符合 0 ... radix digits - 1范围内的整数。


Does the C++03 standard guarantee that sufficiently small non-zero integers are represented exactly in double? If not, what about C++11? Note, I am not assuming IEEE compliance here.

I suspect that the answer is no, but I would love to be proved wrong.

When I say sufficiently small, I mean, bounded by some value that can be derived from the guarantees of C++03, and maybe even be calculated from values made available via std::numeric_limits<double>.


EDIT:

It is clear (now that I have checked) that std::numeric_limits<double>::digits is the same thing as DBL_MANT_DIG, and std::numeric_limits<double>::digits10 is the same thing as DBL_DIG, and this is true for both C++03 and C++11.

Further more, C++03 defers to C90, and C++11 defers to C99 with respect to the meaning of DBL_MANT_DIG and DBL_DIG.

Both C90 and C99 states that the minimum allowable value for DBL_DIG is 10, i.e., 10 decimal digits.

The question then is, what does that mean? Does it mean that integers of up to 10 decimal digits are guaranteed to be represented exactly in double?

In that case, what is then the purpose of DECIMAL_DIG in C99, and the following remark in C99 §5.2.4.2.2 / 12?

Conversion from (at least) double to decimal with DECIMAL_DIG digits and back should be the identity function.


Here is what C99 §5.2.4.2.2 / 9 has to say about DBL_DIG:

Number of decimal digits, 'q', such that any floating-point
number with 'q' decimal digits can be rounded into a
floating-point number with 'p' radix 'b' digits and back again
without change to the q decimal digits,

    { p * log10(b)              if 'b' is a power of 10
    {
    { floor((p-1) * log10(b))   otherwise

FLT_DIG   6
DBL_DIG   10
LDBL_DIG  10

I'll be happy if someone can help me unpack this.

解决方案

Well, 3.9.1 [basic.fundamental] paragraph 8 states

... The value representation of floating-point types is implementation-defined. ...

At least, the implementation has to define what representation it uses.

On the other hand, std::numeric_limits<F> defines a couple of members which seem to imply that the representation is some in the form of significand radix exponent:

  • std::numeric_limits<F>::radix: the radix of the exponent
  • std::numeric_limtis<F>::digits: the number of radix digits

I think these statements imply that you can represent integers in the range of 0 ... radix digits - 1 exactly.

这篇关于C ++ 03保证的double是否正好表示小整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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