0

double can represent every value a float can represent.

Does converting float to double simply extend the mantissa by adding 0 and extend the exponent part by filling sign bits?

I tested some data at 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?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Joe C
  • 2,757
  • 2
  • 26
  • 46
  • 7
    I think you're asking about IEEE-754 (the most common floating-point format), not the C language. The latter has essentially nothing to do with it. – Oliver Charlesworth Jul 17 '17 at 17:31

1 Answers1

6

This has very little to do with C; its only guarantee is that converting from float to double must preserve the value.

So you're really asking about (presumably) 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.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
  • Given a float value, can we still know it is a denormal number or normal number? I had thought all denormal numbers are rounded into the closest normal number. And all normal numbers in float must be normal in double. – Joe C Jul 17 '17 at 17:39
  • Could you please give an example of the bias issue at exponents part? – Joe C Jul 17 '17 at 17:40
  • @JoeC - The exponent is defined as always positive. It's possible that the conversion works out equivalent to sign extension, but I haven't thought it through fully ;) – Oliver Charlesworth Jul 17 '17 at 17:41
  • 2
    @JoeC - If the exponent is zero and the mantissa is non-zero, then it's denormal. – Oliver Charlesworth Jul 17 '17 at 17:42