有几个关于浮点表示法的问题被提交给了SO。例如,十进制数0.1没有精确的二进制表示,因此使用==操作符将其与另一个浮点数进行比较是危险的。我理解浮点表示法背后的原理。

我不明白的是,为什么从数学的角度来看,小数点右边的数字比左边的数字更“特殊”?

例如,数字61.0具有精确的二进制表示,因为任何数字的整数部分总是精确的。但6.10这个数字并不准确。我所做的只是把小数点移了一位突然间我就从精确乌托邦变成了不精确镇。从数学上讲,这两个数字之间不应该有本质差别——它们只是数字。

相比之下,如果我把小数点向另一个方向移动一位,得到数字610,我仍然在Exactopia。我可以继续往这个方向(6100,610000000,610000000000000)它们仍然是完全,完全,完全的。但是一旦小数点越过某个阈值,这些数字就不再精确了。

这是怎么呢

编辑:为了澄清,我不想讨论诸如IEEE之类的行业标准表示,而是坚持我所相信的数学上的“纯粹”方式。以10为基数,位置值为:

... 1000  100   10    1   1/10  1/100 ...

在二进制中,它们将是:

... 8    4    2    1    1/2  1/4  1/8 ...

这些数字也没有任意的限制。位置向左和向右无限增加。


当前回答

我不想重复其他20个答案的总结,所以我只简单地回答:

答案在你的内容中:

为什么以两为基数的数字不能精确地表示一定的比率?

出于同样的原因,小数不足以表示某些比率,即分母中包含除2或5之外的素数因子的不可约分数,至少在其小数展开的尾数中总是有一个不确定的字符串。

为什么十进制数不能精确地用二进制表示?

This question at face value is based on a misconception regarding values themselves. No number system is sufficient to represent any quantity or ratio in a manner that the thing itself tells you that it is both a quantity, and at the same time also gives the interpretation in and of itself about the intrinsic value of the representation. As such, all quantitative representations, and models in general, are symbolic and can only be understood a posteriori, namely, after one has been taught how to read and interpret these numbers.

由于模型是主观的东西,在反映现实的范围内是正确的,我们不需要严格地将二进制字符串解释为2的负幂和正幂的和。相反,我们可以观察到,我们可以创建一组任意的符号,这些符号以2为基底或任何其他基底来精确地表示任何数字或比例。只要考虑一下,我们可以用一个词甚至一个符号来指代无穷大,而不需要“显示无穷大”本身。

As an example, I am designing a binary encoding for mixed numbers so that I can have more precision and accuracy than an IEEE 754 float. At the time of writing this, the idea is to have a sign bit, a reciprocal bit, a certain number of bits for a scalar to determine how much to "magnify" the fractional portion, and then the remaining bits are divided evenly between the integer portion of a mixed number, and the latter a fixed-point number which, if the reciprocal bit is set, should be interpreted as one divided by that number. This has the benefit of allowing me to represent numbers with infinite decimal expansions by using their reciprocals which do have terminating decimal expansions, or alternatively, as a fraction directly, potentially as an approximation, depending on my needs.

其他回答

数字61.0确实有一个精确的浮点运算——但这并不是对所有整数都适用。如果您编写了一个循环,将一个双精度浮点数和一个64位整数都加了1,最终您将达到这样的情况:64位整数完美地表示一个数字,而浮点数却不能——因为没有足够的有效位。

只是在小数点右边求近似值要容易得多。如果你把所有的数字都写成二进制浮点数,这就更有意义了。

另一种思考的方式是,当你注意到61.0完全可以用10为底表示时,移动小数点并不会改变这一点,你是在执行10的幂乘法(10^1,10^-1)。在浮点数中,乘以2的幂并不影响数字的精度。试着用61.0反复除以3来说明一个非常精确的数字是如何失去它的精确表示的。

如果你有足够的空间,十进制数可以精确地表示出来——只是不能用浮点二进制数表示。如果您使用浮点小数点类型(例如System。. net中的十进制),那么许多不能用二进制浮点数精确表示的值都可以被精确表示。

让我们从另一个角度来看——以10为基数,你可能会觉得舒服,你不能准确地表示1/3。这是0.3333333……(重复)。不能将0.1表示为二进制浮点数的原因与此完全相同。你可以表示3 9和27,但不是1/3 1/9或1/27。

问题是3是质数,不是10的因数。当你想将一个数乘以3时,这不是一个问题:你总是可以乘以一个整数而不会遇到问题。但是当你除以一个质数而不是底数的因数时,你就会遇到麻烦(如果你试图用1除以这个数,你就会遇到麻烦)。

虽然0.1通常被用作精确十进制数的最简单例子,它不能用二进制浮点数精确表示,但可以说0.2是一个更简单的例子,因为它是1/5,而5是导致十进制和二进制之间存在问题的素数。


边注:处理有限表示的问题:

Some floating decimal point types have a fixed size like System.Decimal others like java.math.BigDecimal are "arbitrarily large" - but they'll hit a limit at some point, whether it's system memory or the theoretical maximum size of an array. This is an entirely separate point to the main one of this answer, however. Even if you had a genuinely arbitrarily large number of bits to play with, you still couldn't represent decimal 0.1 exactly in a floating binary point representation. Compare that with the other way round: given an arbitrary number of decimal digits, you can exactly represent any number which is exactly representable as a floating binary point.

(注意:我将在这里添加'b'来表示二进制数。其他数字均为十进制)

一种思考方法是用科学记数法。我们习惯看到用科学符号表示的数字,比如6.022141 * 10^23。浮点数内部使用类似的格式存储——尾数和指数,但使用2的幂而不是10。

你的61.0可以重写为1.90625 * 2^5,或者1.11101b * 2^101b加上尾数和指数。把它乘以10(移动小数点),我们可以这样做:

(1.90625 * 2 ^ 5) * 1.25 * 2 ^ (3) = 2.3828125 * 2 ^ (8) = 1.19140625 * 2 ^ (9)

或者在二进制中用尾数和指数:

(1.1110b * 2^101b) * (1.01b * 2^11b) = (10.011000b * 2^1000b) = (1.0011000b * 2^1001b)

Note what we did there to multiply the numbers. We multiplied the mantissas and added the exponents. Then, since the mantissa ended greater than two, we normalized the result by bumping the exponent. It's just like when we adjust the exponent after doing an operation on numbers in decimal scientific notation. In each case, the values that we worked with had a finite representation in binary, and so the values output by the basic multiplication and addition operations also produced values with a finite representation.

现在,考虑一下我们如何用61除以10。我们先把尾数分成1.90625和1.25。小数是1.525,一个很短的数。但是如果我们把它转换成二进制呢?我们会用通常的方法来做——尽可能减去2的最大幂,就像把整数小数转换成二进制一样,但我们将使用2的负幂:

1.525         - 1*2^0   --> 1
0.525         - 1*2^-1  --> 1
0.025         - 0*2^-2  --> 0
0.025         - 0*2^-3  --> 0
0.025         - 0*2^-4  --> 0
0.025         - 0*2^-5  --> 0
0.025         - 1*2^-6  --> 1
0.009375      - 1*2^-7  --> 1
0.0015625     - 0*2^-8  --> 0
0.0015625     - 0*2^-9  --> 0
0.0015625     - 1*2^-10 --> 1
0.0005859375  - 1*2^-11 --> 1
0.00009765625...

哦哦。现在我们有麻烦了。原来,1.90625 / 1.25 = 1.525,用二进制表示时是一个重复分数:1.1110b / 1.01b = 1.10000110011…b我们的机器只有这么多位来容纳尾数,所以它们会四舍五入,假设超过某一点是零。当你用61除以10时,你看到的错误是:

1.100001100110011001100110011001100110011……B * 2^10b 而且,说: 1.100001100110011001100110b * 2^10b

正是尾数的舍入导致了我们与浮点值相关的精度损失。即使当尾数可以精确地表示(例如,当只是两个数字相加时),如果在标准化指数后尾数需要太多数字来拟合,我们仍然会得到数字损失。

实际上,我们一直在做这样的事情,当我们把小数四舍五入到一个可管理的大小时,只给出它的前几位。因为我们用十进制表示结果,所以感觉很自然。但是如果我们四舍五入一个小数,然后把它转换成不同的底数,它看起来就像我们通过浮点四舍五入得到的小数一样难看。

正如我们一直在讨论的,在浮点算术中,十进制0.1不能完美地用二进制表示。

浮点和整数表示形式为所表示的数字提供网格或格子。当完成算术运算时,结果会从网格中脱落,必须通过舍入将其放回网格中。例如二进制网格上的1/10。

如果我们像一位先生建议的那样,使用二进制编码的十进制表示,我们能在网格上保持数字吗?

上面的高分答案完全正确。

首先,你的问题中混合了以2为底和以10为底的数,然后当你把一个不能整除的数放在右边时,你就有问题了。比如十进制的1/3因为3不能整除10的幂,或者二进制的1/5不能整除2的幂。

Another comment though NEVER use equal with floating point numbers, period. Even if it is an exact representation there are some numbers in some floating point systems that can be accurately represented in more than one way (IEEE is bad about this, it is a horrible floating point spec to start with, so expect headaches). No different here 1/3 is not EQUAL to the number on your calculator 0.3333333, no matter how many 3's there are to the right of the decimal point. It is or can be close enough but is not equal. so you would expect something like 2*1/3 to not equal 2/3 depending on the rounding. Never use equal with floating point.