c - float vs. double precision -
the following 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); printf("z=%20.18f\n", z);
will give output
x=3.141593 z=3.141593 x=3.141592741012573242 z=3.141592653589793116
where on third line of output 741012573242
garbage , on fourth line 116
garbage. doubles have 16 significant figures while floats have 7 significant figures? why don't doubles have 14 significant figures?
floating point numbers in c use ieee 754 encoding.
this type of encoding uses sign, significand, , exponent.
because of encoding, many numbers have small changes allow them stored.
also, number of significant digits can change since binary representation, not decimal one.
single precision (float) gives 23 bits of significand, 8 bits of exponent, , 1 sign bit.
double precision (double) gives 52 bits of significand, 11 bits of exponent, , 1 sign bit.
Comments
Post a Comment