'float' vs. 'double' precision

后端 未结 6 819
情话喂你
情话喂你 2020-11-22 14:56

The code

float x  = 3.141592653589793238;
double z = 3.141592653589793238;
printf(\"x=%f\\n\", x);
printf(\"z=%f\\n\", z);
printf(\"x=%20.18f\\n\", x);
print         


        
6条回答
  •  孤独总比滥情好
    2020-11-22 15:38

    Floating point numbers in C use IEEE 754 encoding.

    This type of encoding uses a sign, a significand, and an exponent.

    Because of this encoding, many numbers will have small changes to allow them to be stored.

    Also, the number of significant digits can change slightly since it is a binary representation, not a decimal one.

    Single precision (float) gives you 23 bits of significand, 8 bits of exponent, and 1 sign bit.

    Double precision (double) gives you 52 bits of significand, 11 bits of exponent, and 1 sign bit.

提交回复
热议问题