Number of significant digits for a floating point type

前端 未结 4 1691
梦谈多话
梦谈多话 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条回答
  •  萌比男神i
    2020-12-05 21:34

    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.

提交回复
热议问题