向下转换为float:是否保证溢出行为? [英] Downcasting double to float: is overflow behaviour guaranteed?
问题描述
如果我尝试这个
float f =(float)numeric_limits< double> :: infinity
或者,尝试将大于float max的任何数据转换为浮点数,
它适用于GCC, / div>
float f =(float)numeric_limits< double> :: infinity();
如果您的编译平台为浮点计算提供IEEE 754算法,则可以将 f
设置为无穷大它通常做)。
或者,试着将比float max更大的任何值转换为浮点值,无限?
否。在默认的IEEE 754 round-to-nearest模式中,一些 double
的值高于最大有限 float
, FLT_MAX
)转换为 FLT_MAX
。确切的限制是 FLT_MAX
(C99十六进制表示中的 0x1.fffffep127
)和下一个 float
如果单精度格式的指数具有较大范围, 0x2.0p128
,则可以表示的数字。因此,限额为 0x1.ffffffp127
或约为3.4028235677973366e + 38的十进制。
If I try this
float f = (float)numeric_limits<double>::infinity();
Or indeed, try to cast anything bigger than float max down to a float, am I guaranteed to end up with infinity?
It works on GCC, but is it a standard though?
float f = (float)numeric_limits<double>::infinity();
This is guaranteed to set f
to infinity if your compilation platform offers IEEE 754 arithmetic for floating-point computations (it usually does).
Or indeed, try to cast anything bigger than float max down to a float, am I guaranteed to end up with infinity?
No. In the default IEEE 754 round-to-nearest mode, a few double
values above the maximum finite float
(that is, FLT_MAX
) convert to FLT_MAX
. The exact limit is the number midway between FLT_MAX
(0x1.fffffep127
in C99 hexadecimal representation) and the next float
number that could be represented if the exponent in the single-precision format had a larger range, 0x2.0p128
. The limit is thus 0x1.ffffffp127
or approximately 3.4028235677973366e+38 in decimal.
这篇关于向下转换为float:是否保证溢出行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!