Elements From a Matrix A bit Off

I am a new user of Matlab and I am sure there must be a good reason for this. Here are the code and output: >> a=[0:0.1:0.5]; >> b=0.5-a; >> [a(2) b(5)]
ans =
0.1 0.1
The two numbers look the same in this case. However, when I checked: >> a(2)==b(5)
ans =
0
And
>> a(2)-b(5)
ans =
2.7756e-017
They are actually slightly off. Does anyone know why?
Also, how can I prevent the unwanted consequences from using elements from the matrix, i.e. b(5) is actually not exactly 0.1? Right now, I am just rounding it, which woudl be a bit annoying from time to time. Hope there are better ways to do this.
Cheers!
Fred

この質問は閉じられています。

製品

質問済み:

2013 年 11 月 19 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by