C 浮点数的精度 [英] Precision in C floats
问题描述
通常我们说浮点数的精度为小数点后 6 位.但是如果我们存储大量 10^30 的顺序,我们将不会得到小数点后 6 位.那么说浮点数的精度为小数点后6位是否正确?
Generally we say that a float has precision of 6 digits after the decimal point. But if we store a large number of the order of 10^30 we won't get 6 digits after the decimal point. So is it correct to say that floats have a precision of 6 digits after the decimal point?
推荐答案
小数点后6位"是废话,你的例子就是很好的证明.
"6 digits after the decimal point" is nonesnse, and your example is a good demonstration of this.
这是 float
数据的精确规范输入.
This is an exact specification of the float
data type.
float
的精度为 24 位.根据在线资料,有 23 位表示二进制点之后的分数,此外还有一个隐式前导位".这总共给出了 24 个有效位.
The precision of the float
is 24 bits. There are 23 bits denoting the fraction after the binary point, plus there's also an "implicit leading bit", according to the online source. This gives 24 significant bits in total.
因此在十进制数字中,这大约是:
Hence in decimal digits this is approximately:
24 * log(2)/log(10) = 7.22
24 * log(2) / log(10) = 7.22
这篇关于C 浮点数的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!