-2

I was executing following C code-

float a=3.25678932;
printf("%0.10f\n",a);

The above printf() statement giving following output:

3.2567892075

I am not able to understand how other four digits('2075') are coming after first six digits('256789') after decimal.

Mat
  • 202,337
  • 40
  • 393
  • 406

1 Answers1

2

Your C implementation likely uses the IEEE-754 binary32 format for float. In this format, every finite number is, in effect, a sign (+ or −) applied to a 24-bit integer M multiplied by some power of two.

Since 21 ≤ 3.25678932 < 22, the power of two used is 22−24 = 2−22. (The −24 scales the 24-bit integer to between ½ and 1, and the 2 scales that to between 2 and 4.)

Then, to choose the integer M, we choose the integer that is closest to 3.25678932/2−22. 3.25678932/2−22 is about 13,659,964.472, so we use 13,659,964.

Thus, the result of converting 3.25678932 to float is 13,659,964•2−22, which is exactly 3.25678920745849609375.

When you print this rounded to 10 digits after the decimal point, the result is “3.2567892075”.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312