为什么100 * 0.07等于6.9999 ....? [英] Why is the 100*0.07 equal to 6.9999....?

查看:145
本文介绍了为什么100 * 0.07等于6.9999 ....?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一个朋友在Matlab中写了以下内容,输出有点奇怪:

One of my friends wrote the following in Matlab and the outputs are little weird:

for p=0.01:0.01:0.1
100*p 
end

以下是输出:

1
2
3
4 
5
6.000000000000001
6.999999999999999
8
9
10

我想知道为什么会有轻微的错误?这是否意味着在一般情况下的准确性也与在这种情况下一样差?

I'd like to know why there is a slight error? Does this mean that, the accuracy in the general case is also as poor as it is in this case?

我们比较了数字-7==6.999999999999999,输出为0.因此,Matlab自相矛盾!

We compared the numbers -- 7==6.999999999999999 and the output was 0. So, Matlab contradicts itself!

推荐答案

问题是0.01不能精确地表示为二进制浮点数. 0.07都不能.

The problem is that 0.01 cannot be exactly represented in binary floating-point. Neither can 0.07.

这篇关于为什么100 * 0.07等于6.9999 ....?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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