简体   繁体   English

如何将float转换为double?

[英]How does converting float to double work?

double can represent every value a float can represent. double可以表示float可以表示的每个值。

Does converting float to double simply extend the mantissa by adding 0 and extend the exponent part by filling sign bits? 将float转换为double会简单地通过加0扩展尾数并通过填充符号位来扩展指数部分吗?

I tested some data at http://www.binaryconvert.com/index.html . 我在http://www.binaryconvert.com/index.html上测试了一些数据。 It works in this way. 它以这种方式工作。 But I did not find any official definition of the conversion. 但是我没有找到有关转换的任何正式定义。 Does the conversion have any corner case not working this way? 转换中是否有任何极端情况无法通过这种方式工作?

This has very little to do with C; 这与C无关。 its only guarantee is that converting from float to double must preserve the value. 它唯一的保证是从floatdouble转换必须保留该值。

So you're really asking about (presumably) IEEE-754 . 因此,您实际上是在询问(大概是) IEEE-754 There are at least a few ways in which your description doesn't hold: 至少有几种方法不适合您的描述:

  1. The exponents for single-precision and double-precision have different biases. 单精度和双精度的指数有不同的偏差。 So conversion requires more than just sign extension. 因此,转换不仅需要符号扩展。

  2. Denormal numbers require special handling (they become normal numbers). 非正常数字需要特殊处理(它们变为正常数字)。

  3. NaNs may also require special handling - distinguishing between signalling and quiet NaNs is dependent on the value of the mantissa. NaN还可能需要特殊处理-区分信号和安静的NaN取决于尾数的值。

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

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