Assuming 64-bit IEEE double, there is a 52-bit mantissa and 11-bit exponent. Look at the following numbers:
1.0000 00000000 00000000 00000000 00000000 00000000 00000000 × 2^0 = 1
The smallest representable number greater than 1:
1.0000 00000000 00000000 00000000 00000000 00000000 00000001 × 2^0 = 1 + 2^-52
Therefore:
epsilon = (1 + 2^-52) - 1 = 2^-52
Are there any numbers between 0 and epsilon? Plenty... E.g. the minimal positive representable (normal) number is:
1.0000 00000000 00000000 00000000 00000000 00000000 00000000 × 2^-1022 = 2^-1022
In fact there are about (1022 - 52 + 1)×2^52 = 4372995238176751616 numbers between 0 and epsilon, which is about 47% of all the positive representable numbers...
Compare double to zero using epsilon
So, be careful to use:
double someValue = ...
if (someValue < std::numeric_limits<double>::epsilon() &&
someValue > -std::numeric_limits<double>::epsilon()) {
someValue = 0.0;
}
本文深入探讨了64位IEEE双精度浮点数的表示范围,特别是最小可表示的正数与零之间的数值。通过数学推导,揭示了在最小可表示的正数1+2^-52与零之间存在着大量的数值,数量约为所有正可表示数的47%。这一发现对于理解浮点数的精度和避免计算中可能产生的误差至关重要。

被折叠的 条评论
为什么被折叠?



