简体   繁体   English

在float和double之间键入NaN的转换

[英]Type conversion of NaN between float and double

Do the c++ standards define the behavior of casting a NaN from float to double or vice-versa, or such outcome is implementation-defined or undefined? c ++标准是否定义了将NaN从float转换为double或反之亦然的行为,或者这样的结果是实现定义的还是未定义的?

The architecture I'm pragmatically concerned with is x86-64 with GCC, but I'd appreciate a broader theoretical understanding. 我务实关注的架构是x86-64与GCC,但我欣赏更广泛的理论理解。

There's nothing explicit in the C++ standard that addresses conversions of NaNs, so you have to rely on the standard conversions. C ++标准中没有任何明确的解决NaN转换的方法,因此您必须依赖标准转换。 In particular, floats can be promoted to double and the value is unchanged, and doubles converted to floats will have the same value if it can be represented in the smaller type. 特别是,浮点数可以提升为double,值不变,如果可以用较小的类型表示,则转换为浮点数的双精度值将具有相同的值。 Since both types support a NaN, the value should stay a NaN. 由于两种类型都支持NaN,因此该值应保持为NaN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM