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