10

The description for type float in C mentions that the number of significant digits is 6. However,

float f = 12345.6;

and then printing it using printf() does not print 12345.6, it prints 12345.599609. So what does "6 significant digits" (or "15 in case of a double") mean for a floating point type?

2
  • 9
    Be aware that floating point types are not stored in base 10, so the number of significant decimal digits is necessarily an approximation. Oct 10, 2012 at 8:37
  • 2
    Actually there are 8 bits for exp and 23 bits for frac.
    – halfelf
    Oct 10, 2012 at 8:39

4 Answers 4

16

6 significant digits means that the maximum error is approximately +/- 0.0001%. The single float value actually has about 7.2 digits of precision (source). This means that the error is about +/- 12345.6/10^7 = 0.00123456. Which is on the order of your error (0.000391).

12

According to the standard, not all decimal number can be stored exactly in memory. Depending on the size of the representation, the error can get to a certain maximum. For float this is 0.0001% (6 significant digits = 10^-6 = 10^-4 %).

In your case the error is (12345.6 - 12345.599609) / 12345.6 = 3.16e-08 far lower than the maximum error for floats.

8

What you're seeing is not really any issue with significant digits, but the fact that numbers on a computer are stored in binary, and there is no finite binary representation for 3/5 (= 0.6). 3/5 in binary looks like 0.100110011001..., with the "1001" pattern repeating forever. This sequence is equivalent to 0.599999... repeating. You're actually getting to three decimal places to the right of the decimal point before any error related to precision kicks in.

This is similar to how there is no finite base-10 representation of 1/3; we have 0.3333 repeating forever.

0

The problem here is that you cannot assure a number can be stored in a float. You need to represent this number with mantissa, base and exponent as IEEE 754 explains. The number printf(...) shows you is the real float number that was stored. You can not assure a number of significant digits in a float number.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.