Search code examples
cfloating-pointfloating-point-conversion

Number of significant digits for a floating point type


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?


Solution

  • 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.