Number of significant digits for a floating point type

前端 未结 4 1677
梦谈多话
梦谈多话 2020-12-05 20:51

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

float f = 12345.6;
4条回答
  •  醉梦人生
    2020-12-05 21:19

    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.

提交回复
热议问题