You can only represent numbers exactly in IEEE754 (at least for the single and double precision binary formats) if they can be constructed from adding together inverted powers of two (i.e., 2-n
like 1
, 1/2
, 1/4
, 1/65536
and so on) subject to the number of bits available for precision.
在浮点数(23 位精度)提供的缩放范围内,没有任何 2 的倒幂组合可以精确地达到 101.1or双精度(52 位精度)。
如果您想了解有关二次幂反转的工作原理的快速教程,请参阅这个答案 https://stackoverflow.com/questions/3448777/how-to-represent-0-1-in-floating-point-arithmetic-and-decimal/3448855#3448855.
将该答案中的知识应用到您的101.1
数字(作为单精度浮点数):
s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm 1/n
0 10000101 10010100011001100110011
| | | || || || |+- 8388608
| | | || || || +-- 4194304
| | | || || |+----- 524288
| | | || || +------ 262144
| | | || |+--------- 32768
| | | || +---------- 16384
| | | |+------------- 2048
| | | +-------------- 1024
| | +------------------ 64
| +-------------------- 16
+----------------------- 2
尾数部分实际上永远持续下去101.1
:
mmmmmmmmm mmmm mmmm mmmm mm
100101000 1100 1100 1100 11|00 1100 (and so on).
因此,这不是精度问题,没有多少有限位可以准确地以 IEEE754 格式表示该数字。
Using the bits to calculate the actual number (closest approximation), the sign is positive. The exponent is 128+4+1 = 133 - 127 bias = 6, so the multiplier is 26 or 64.
The mantissa consists of 1 (the implicit base) plus (for all those bits with each being worth 1/(2n) as n starts at 1 and increases to the right), {1/2, 1/16, 1/64, 1/1024, 1/2048, 1/16384, 1/32768, 1/262144, 1/524288, 1/4194304, 1/8388608}
.
当你把所有这些加起来时,你会得到1.57968747615814208984375
.
当您将其乘以之前计算的乘数时,64
, 你得到101.09999847412109375
.
所有数字的计算方法为bc
使用 100 位十进制数字的比例,导致大量尾随零,因此数字should非常准确。更是如此,因为我用以下方法检查了结果:
#include <stdio.h>
int main (void) {
float f = 101.1f;
printf ("%.50f\n", f);
return 0;
}
which also给我101.09999847412109375000...
.