当通过C / C ++中的双精度运算时,浮点是否保证被保留? [英] Is a float guaranteed to be preserved when transported through a double in C/C++?

查看:113
本文介绍了当通过C / C ++中的双精度运算时,浮点是否保证被保留?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设符合 IEEE-754 标准,是否通过双重传输保证保留浮动?

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

假设 f 可以获得IEEE定义的任何特殊值,例如NaN和Infinity。

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

根据IEEE,是否有一种情况,其中断言将被满足,位元级表示在通过double传输之后不会保留?

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?

代码片段在C和C ++中都有效。

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

推荐答案

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

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


float double



的值集合
的子集。 p>和其他C和C ++标准说的等同的东西。据我所知,NaN和无穷大是 float 类型的值,尽管用作操作数时带有一些特殊情况规则的值。

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.

事实上,float - > double - > float转换恢复了 float 的原始值。如果数字转换在目标类型中可表示,则它们都保留该值。

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.

位级表示是一个稍微不同的事情。想象一下,有一个值 float 有两个不同的逐位表示。然后C标准中没有什么东西阻止了float - > double - > float转换从一个切换到另一个。在IEEE中,除非有填充位,否则不会发生实际值,但是我不知道IEEE是否排除具有明确的逐位表示的单个NaN。 NaNs不会相比于自己,所以也没有标准的方法来判断两个NaN是相同的NaN还是不同的NaNs,除了可能将它们转换为字符串。

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.

这篇关于当通过C / C ++中的双精度运算时,浮点是否保证被保留?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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