Colon operator numerical error

2 views (last 30 days)
Rowland
Rowland on 13 May 2011
When I create a vector using the colon operator I noticed for certain vectors there is a small numerical error in the values returned. For example: >> a = 0:.1:1
a =
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
>> a(4)
ans =
0.3
>> a(4) == .3
ans =
0
>> a(4) - .3
ans =
5.5511e-17
I find this very strange. Do other people have this same problem?

Accepted Answer

Walter Roberson
Walter Roberson on 13 May 2011
Yes, everyone has this problem. Please read this FAQ
  2 Comments
Rowland
Rowland on 13 May 2011
Good to know. Thanks!
Jan
Jan on 13 May 2011
To be exact: I have the same *behaviour*, which is caused by the underlying representation of numbers. A *problem* occurs only, if a user has not matching expectations.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!