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

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


当前回答

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

首先,你的问题中混合了以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.

其他回答

你不能用二进制精确地表示0.1,就像你不能用传统的英国尺测量0.1英寸一样。

英国的尺子,就像二进制分数一样,都是关于一半的。你可以测量半英寸,或四分之一英寸(当然是一半),或八分之一,或十六分之一,等等。

If you want to measure a tenth of an inch, though, you're out of luck. It's less than an eighth of an inch, but more than a sixteenth. If you try to get more exact, you find that it's a little more than 3/32, but a little less than 7/64. I've never seen an actual ruler that had gradations finer than 64ths, but if you do the math, you'll find that 1/10 is less than 13/128, and it's more than 25/256, and it's more than 51/512. You can keep going finer and finer, to 1024ths and 2048ths and 4096ths and 8192nds, but you will never find an exact marking, even on an infinitely-fine base-2 ruler, that exactly corresponds to 1/10, or 0.1.

不过,你会发现一些有趣的事情。让我们看看我列出的所有近似值,对于每一个近似值,明确地记录0.1是大是小:

fraction decimal 0.1 is... as 0/1
1/2 0.5 less 0
1/4 0.25 less 0
1/8 0.125 less 0
1/16 0.0625 greater 1
3/32 0.09375 greater 1
7/64 0.109375 less 0
13/128 0.1015625 less 0
25/256 0.09765625 greater 1
51/512 0.099609375 greater 1
103/1024 0.1005859375 less 0
205/2048 0.10009765625 less 0
409/4096 0.099853515625 greater 1
819/8192 0.0999755859375 greater 1

现在,如果向下读最后一列,就会得到0001100110011。1/10的无限重复二进制分数是0.0001100110011,这不是巧合……

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

你们知道整数,对吧?每一位代表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

根(数学)原因是,当你处理整数时,它们是可数无限的。

这意味着,即使它们的数量是无限的,我们也可以“数出”序列中的所有项目,而不会跳过任何一项。这意味着,如果我们想要在列表中的第610000000000000th位置上得到一项,我们可以通过一个公式来计算它。

然而,实数是无限的。你不能说“给我位置610000000000000的真实数字”并得到一个答案。原因是,即使在0到1之间,当考虑浮点值时,也有无限个值。这同样适用于任何两个浮点数。

更多信息:

http://en.wikipedia.org/wiki/Countable_set

http://en.wikipedia.org/wiki/Uncountable_set

更新: 很抱歉,我似乎误解了这个问题。我的回答是关于为什么我们不能表示每一个真实的值,我没有意识到浮点数被自动归类为理性。

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

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

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