有几个关于浮点表示法的问题被提交给了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 ...

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


当前回答

我很惊讶居然没有人说过:使用连分式。任何有理数都可以用二进制有限地表示。

一些例子:

1/3 (0.3333...)

0; 3

5/9 (0.5555...)

0; 1, 1, 4

10/43 (0.232558139534883720930...).

0; 4, 3, 3

9093/18478 (0.49209871198181621387596060179673...).

0; 2, 31, 7, 8, 5

从这里开始,有多种已知的方法可以在内存中存储整数序列。

除了精确地存储数字外,连分式还有其他一些好处,比如最佳有理逼近。如果您决定提前终止连分式中的数字序列,则剩余的数字(当重新组合为分数时)将给出可能的最佳分数。这是如何找到圆周率的近似值的:

π的连分式:

3; 7, 15, 1, 292 ...

在1处终止序列,得到的分数是:

355/113

这是一个很好的有理近似。

其他回答

有理数的数量是无限的,而用来表示有理数的比特的数量是有限的。见http://en.wikipedia.org/wiki/Floating_point # Accuracy_problems。

你们知道整数,对吧?每一位代表2^n

2 ^ 4 = 16 2 ^ 3 = 8 2 ^ 2 = 4 2 ^ 1 = 2 2 ^ 0 = 1

浮点数也是一样的(有一些区别),但是比特代表2^-n 2 ^ 1 = 1/2 = 0.5 2 ^ 2 = 1 / (2 * 2) = 0.25 2 ^ 3 = 0.125 2 ^ 4 = 0.0625

浮点二进制表示法:

符号指数分数(我认为无形的1被附加到分数) B11 b10 b9 b8 b7 b6 b5 b4 b3 b2 b1 b0

一个简单的答案是:计算机没有无限的内存来存储分数(在以科学记数法的形式表示十进制数之后)。根据IEEE 754双精度浮点数标准,我们只有53位的限制来存储分数。 欲了解更多信息:http://mathcenter.oxford.emory.edu/site/cs170/ieee754/

我不想重复其他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.

重复一下我在给斯基特先生的评论中所说的话:我们可以用十进制表示1/3、1/9、1/27或任何有理数。我们通过添加一个额外的符号来实现。例如,在数字的十进制展开中重复的数字上的一行。将十进制数表示为二进制数序列所需要的是1)一个二进制数序列,2)一个基数点,以及3)一些其他符号来表示序列的重复部分。

赫纳的引用符号就是一种方法。他用引号表示序列中重复的部分。文章地址:http://www.cs.toronto.edu/~hehner/ratno.pdf,维基百科词条:http://en.wikipedia.org/wiki/Quote_notation。

并没有说我们不能在表示系统中添加一个符号,所以我们可以用二进制引号表示十进制有理数,反之亦然。