C : how is double number (e.g. 123.45) stored in a float variable or double variable or long double variable?

前端 未结 3 1731
情书的邮戳
情书的邮戳 2020-12-11 10:00
#include 

int main () {
float a = 123.0; 

unsigned char ch, *cp;
ch = 0xff;

cp = (unsigned char *) &a;
printf (\"%02x\", ch&(*(cp+3)));
pri         


        
3条回答
  •  北海茫月
    2020-12-11 10:55

    See Wikipedia: http://en.wikipedia.org/wiki/Single_precision_floating-point_format, which describes single precision floating point (a typical C float, but depends on the compiler) as a 1-bit sign, 8-bit biased exponent, and 24-bit mantissa (23-bits stored).

    For your example:

    123.0 = 42f60000hex = 0 10000101 11101100000000000000000bin
    1-bit sign = 0 (indicating positive number)
    8-bit biased exponent = 10000101bin = 133dec - 127dec = 6dec
    23-bit mantissa = 11101100000000000000000bin = 1.111011bin (note implied leading 1)

    Converting 1.111011bin x 26dec = 1111011.0bin = 123.0dec

提交回复
热议问题