是float保证通过双在C / C ++运输时是preserved? [英] Is a float guaranteed to be preserved when transported through a double in C/C++?

查看:103
本文介绍了是float保证通过双在C / C ++运输时是preserved?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IEEE-754 一致性,是通过双运输时保证是preserved一个浮动<? / p>

在换句话说,将下面的断言总是感到满意?

  INT的main()
{
    浮F = some_random_float();
    断言(F ==(浮点)(双)F);
}

假设˚F可以获取任何由IEEE定义的特殊值,如NaN和无穷大。

据IEEE,是那里的断言将得到满足的情况下,但确切位级重presentation无法通过双?运输后preserved

在code段是在C和C ++有效。


解决方案

您甚至不需要承担IEEE。 C89说,在3.1.2.5:


  

集合类型的值浮动是值的集合的子集
  类型双击


和所有其他C和C ++标准等同的东西。据我所知,NaN和无穷是类型的值浮动,尽管作为操作数使用时,一些特殊情况规定值

这是浮动的事实 - >双击 - >浮充转换恢复浮动的原始值从事实如下(一般),该数字转换所有$ P $如果它重新presentable的目标类型pserve值。

位水平再presentations是一个稍微不同的问题。试想一下,有浮动有两种截然不同的按位重新presentations的值。然后什么C标准prevents浮动 - >双击 - >浮充转换转换从一个到另一个。在IEEE会不会发生实际值,除非有填充比特,但我不知道是否IEEE排除一个NaN的具有不同按位重新presentations。 NaN的不比较等于自己反正,所以也告诉2 NaN的是否是同一个NaN的或不同的NaN比,也许它们转换为字符串其他的标准方法。该问题可能是没有实际意义。

有一点需要注意的是不合格的编译器的方式,他们在不断的幕后超precise值,留在浮点寄存器和重复使用没有四舍五入例如中间结果。我不认为这会导致你的榜样code失败,但只要你在做浮点 == 这是你开始的那种东西不必担心。

Assuming IEEE-754 conformance, is a float guaranteed to be preserved when transported through a double?

In other words, will the following assert always be satisfied?

int main()
{
    float f = some_random_float();
    assert(f == (float)(double)f);
}

Assume that f could acquire any of the special values defined by IEEE, such as NaN and Infinity.

According to IEEE, is there a case where the assert will be satisfied, but the exact bit-level representation is not preserved after the transportation through double?

The code snippet is valid in both C and C++.

解决方案

You don't even need to assume IEEE. C89 says in 3.1.2.5:

The set of values of the type float is a subset of the set of values of the type double

And every other C and C++ standard says equivalent things. As far as I know, NaNs and infinities are "values of the type float", albeit values with some special-case rules when used as operands.

The fact that the float -> double -> float conversion restores the original value of the float follows (in general) from the fact that numeric conversions all preserve the value if it's representable in the destination type.

Bit-level representations are a slightly different matter. Imagine that there's a value of float that has two distinct bitwise representations. Then nothing in the C standard prevents the float -> double -> float conversion from switching one to the other. In IEEE that won't happen for "actual values" unless there are padding bits, but I don't know whether IEEE rules out a single NaN having distinct bitwise representations. NaNs don't compare equal to themselves anyway, so there's also no standard way to tell whether two NaNs are "the same NaN" or "different NaNs" other than maybe converting them to strings. The issue may be moot.

One thing to watch out for is non-conforming modes of compilers, in which they keep super-precise values "under the covers", for example intermediate results left in floating-point registers and reused without rounding. I don't think that would cause your example code to fail, but as soon as you're doing floating-point == it's the kind of thing you start worrying about.

这篇关于是float保证通过双在C / C ++运输时是preserved?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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