如果将整数类型转换为浮点类型,反之亦然,在C ++中会发生什么? [英] What happens in C++ when an integer type is cast to a floating point type or vice-versa?

查看:123
本文介绍了如果将整数类型转换为浮点类型,反之亦然,在C ++中会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

底层位是否只是被重新解释为浮点值?还是有运行时转换来产生最接近的浮点值?



字节序在任何平台上都是一个因素吗(即,浮点数的字节序与整数不同)?



不同宽度类型的行为(例如,int浮动还是int两倍)?



什么语言标准是否可以保证此类转换/转换的安全性?



反浮点数到int的转换(或double到int)又如何呢?如果浮点数具有较小的幅度值(例如2),则位模式在解释为int时是否具有相同的含义?

解决方案

底层位是否只是被重新解释为浮点值?



否,该值是根据规则转换的



是否存在运行时转换以产生最接近的浮点值?



是的,存在运行时转换。



对于浮点数->整数,如果源值在以下范围内,则该值将被截断整数类型。如果不是,则行为未定义。至少我认为重要的是来源价值,而不是结果。我必须查一下以确保。如果目标类型为char,则边界情况为 CHAR_MAX + 0.5 。我认为将其强制转换为char是不确定的,但是我不确定。



对于整数->浮点数,结果是完全相同的值可能,或者是整数值两侧的两个浮点值之一。



字节序在任何平台上是否是一个因素(即,浮点数的字节序与整数不同)? p>

不,永远不会。转换是根据值而不是存储表示形式定义的。



不同宽度类型的行为方式如何(例如,将int浮动为整数,将int浮动为double)?



所有重要的是类型的范围和精度。假设32位整数和IEEE 32位浮点数,则int-> float转换可能不精确。假设64位IEEE也加倍,则int-> double转换不可能是不精确的,因为所有int值都可以精确地表示为double。



语言标准对此类强制转换/转换的安全性有何保证?强制转换,我的意思是static_cast或C风格的强制转换。



如上所述,它是安全的,除非将浮点值转换为整数类型,并且该值超出目标类型的范围。



如果浮点数的值较小(例如2),则



不,不是。 IEEE 32位2表示为 0x40000000


Do the underlying bits just get "reinterpreted" as a floating point value? Or is there a run-time conversion to produce the nearest floating point value?

Is endianness a factor on any platforms (i.e., endianness of floats differs from ints)?

How do different width types behave (e.g., int to float vs. int to double)?

What does the language standard guarantee about the safety of such casts/conversions? By cast, I mean a static_cast or C-style cast.

What about the inverse float to int conversion (or double to int)? If a float holds a small magnitude value (e.g., 2), does the bit pattern have the same meaning when interpreted as an int?

解决方案

Do the underlying bits just get "reinterpreted" as a floating point value?

No, the value is converted according to the rules in the standard.

is there a run-time conversion to produce the nearest floating point value?

Yes there's a run-time conversion.

For floating point -> integer, the value is truncated, provided that the source value is in range of the integer type. If it is not, behaviour is undefined. At least I think that it's the source value, not the result, that matters. I'd have to look it up to be sure. The boundary case if the target type is char, say, would be CHAR_MAX + 0.5. I think it's undefined to cast that to char, but as I say I'm not certain.

For integer -> floating point, the result is the exact same value if possible, or else is one of the two floating point values either side of the integer value. Not necessarily the nearer of the two.

Is endianness a factor on any platforms (i.e., endianness of floats differs from ints)?

No, never. The conversions are defined in terms of values, not storage representations.

How do different width types behave (e.g., int to float vs. int to double)?

All that matters is the ranges and precisions of the types. Assuming 32 bit ints and IEEE 32 bit floats, it's possible for an int->float conversion to be imprecise. Assuming also 64 bit IEEE doubles, it is not possible for an int->double conversion to be imprecise, because all int values can be exactly represented as a double.

What does the language standard guarantee about the safety of such casts/conversions? By cast, I mean a static_cast or C-style cast.

As indicated above, it's safe except in the case where a floating point value is converted to an integer type, and the value is outside the range of the destination type.

If a float holds a small magnitude value (e.g., 2), does the bit pattern have the same meaning when interpreted as an int?

No, it does not. The IEEE 32 bit representation of 2 is 0x40000000.

这篇关于如果将整数类型转换为浮点类型,反之亦然,在C ++中会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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