Differences between two vectors that suppose to be equal

Does anyone know why those are not all equal?
a=.1:.1:.9; b=[.1 .2 .3 .4 .5 .6 .7 .8 .9];
a==b
1 1 0 1 1 1 1 1 1
a(3)-b(3)
5.551115123125783e-17

 采纳的回答

2 个评论

From the link above:
The difference is that 0:0.1:0.4 increments by a number very close to but not exactly 0.1 for the reasons mentioned below. So after a few steps it will be off whereas [0 0.1 0.2 0.3 0.4] is forcing the the numbers to their proper value, as accurately as they can be represented anyway.
To piggyback off this answer, try the following:
format hex
a=.1:.1:.9; b=[.1 .2 .3 .4 .5 .6 .7 .8 .9];
[a', b']
This displays the variable in hex format, which is a representation of the internal binary format that Matlab uses to store the value. Comparing across rows, you can see that the 3rd row differs in the last digit between a and b. Hence the reason a(3) ~= b(3).

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息

产品

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by