为什么0.1 + 0.4 = 0.5? [英] Why does 0.1 + 0.4 = 0.5?

查看:204
本文介绍了为什么0.1 + 0.4 = 0.5?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道浮点数已损坏,因为十进制数不能总是用二进制完美表示.它们四舍五入为可以用二进制表示的数字.有时这个数字更高,有时更低.在这种情况下,请使用无处不在的 IEEE 754双重格式 0.1和0.4高一圈:

We know that floating point is broken, because decimal numbers can't always be perfectly represented in binary. They're rounded to a number that can be represented in binary; sometimes that number is higher, and sometimes it's lower. In this case using the ubiquitous IEEE 754 double format both 0.1 and 0.4 round higher:

0.1 = 0.1000000000000000055511151231257827021181583404541015625
0.4 = 0.40000000000000002220446049250313080847263336181640625

由于这两个数字均很高,因此您希望它们的总和也很高.完美的加法应该为您提供0.5000000000000000277555756156289135105907917022705078125,但是您却获得了一个精确的0.5.为什么?


上面已经指出了问题浮点数学是否已损坏? .考虑到该问题的答案时,它要求对非直觉结果进行进一步的详细说明.

Since both of these numbers are high, you'd expect their sum to be high as well. Perfect addition should give you 0.5000000000000000277555756156289135105907917022705078125, but instead you get a nice exact 0.5. Why?


The question Is floating point math broken? was already identified above, but this question is different. It is asking for a further level of detail on a non-intuitive result when taking the answers of that question into consideration.

推荐答案

此计算的行为方式是这样的,因为加法会将结果推到另一个(二进制)数量级.这会在左侧(最重要的一面)增加一个有效位,因此必须在右侧(最不重要的一面)降低一些.

This calculation behaves this way because the addition pushes the result into another (binary) order of magnitude. This adds a significant bit to the left (most-significant side) and therefore has to drop a bit on the right (least-significant side).

第一个数字0.1以二进制形式存储为2^-4 == 1/162^-3 == 1/8之间的数字.第二个数字0.4以二进制形式存储为2^-2 == 1/42^-1 == 1/2之间的数字.总和0.5是数字2^-1 == 1/2或稍大一些.这是幅度上的不匹配,并且可能导致数字丢失.

The first number, 0.1, is stored in binary as a number between 2^-4 == 1/16 and 2^-3 == 1/8. The second number, 0.4, is stored in binary as a number between 2^-2 == 1/4 and 2^-1 == 1/2. The sum, 0.5, is the number 2^-1 == 1/2 or a little larger. This is a mis-match in magnitudes and can cause loss of digits.

这里是一个示例,更易于理解.假设我们正在使用可以在浮点中存储四个十进制数字的十进制计算机.假设我们要添加数字10/320/3.这些可能最终存储为

Here is an example, easier to understand. Let's say we are working on a decimal computer that can store four decimal digits in floating point. Let's also say we want to add the numbers 10/3 and 20/3. These may end up stored as

3.334

6.667

两者都很高.当我们得到这些数字时,我们期望总和也有点高,即

both of which are a little high. When we get those numbers, we expect the sum to be also a little high, namely

10.001

但是请注意,我们的结果已经迈入了一个新的数量级.完整的结果有五个十进制数字,将不适合.因此计算机将结果四舍五入为四位十进制数字并获得总和

but notice that we have moved into a new order of magnitude with our result. The full result has five decimal digits, which will not fit. So the computer rounds the result to just four decimal digits and gets the sum

10.00

令人惊讶的是,这是对10/3 + 20/3的正确准确答案.

which surprisingly is the correct exact answer to 10/3 + 20/3.

在我的美国高中化学和物理课上,我经常得到同样的东西.当计算移至新的数量级时,精确度和有效位数会发生奇怪的事情.

I get the same kind of thing often in my U.S. high-school Chemistry and Physics classes. When a calculation moves to a new order of magnitude, strange things happen with precision and significant digits.

这篇关于为什么0.1 + 0.4 = 0.5?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆