说一下精度float
or double
是一些小数位数是草率的术语。float
and double
通常使用 IEEE-754 二进制 32 和二进制 64 格式实现,它们分别使用 24 位和 53 位的有效数。 (有效数是浮点表示形式的小数部分。它伴随着符号位和指数。)这些格式的精度为 24 位和 53 位。
精度足够,对于float
,任何具有六位有效数字的十进制数字都可以转换为float
足够准确,转换回六位十进制数字会产生相同的数字。 (本文讨论的所有转换都使用舍入到最接近的值。)六是限制;有一些七位十进制数字无法在往返转换后继续存在。为了double
,限制为 15 位有效数字。
Here is how we find the limits. A 24-bit significand can represent 224 values. That is 16,777,216, so it is certainly capable of representing all seven-digit decimal numerals from 0 to 9,999,999. However, if we want to represent some seven-digit decimal numeral a.bcdefg•10n, the last decimal numeral in this interval, 9.999999•10n, has some float
representation f•2e. Because the powers of 10 are irregular relative to the powers of two, that f might be small, meaning that we are not representing the numbers from 0.000000•10n to 9.999999•10n with significands entirely in a single sequence 0 to 16,777,215. (I am glossing over some details here; numbers which would have significands under 222 are rescaled to another exponent in the normal range. But being in a finer exponent range gives more precision, so that is not an issue.) Rather, there was a rollover at some point, and the exponent increased. So we lose one bit of precision. So the number of decimal digits we can guarantee survive a round-trip conversion to a floating-point format with p digits in base b is floor((p−1)•log10b). (That formula is given in C 2018 5.2.4.2.2 12.)