Vector initialized as 0:0.01:100 contains entries that are not exact

Hi,
I have a time vector initialized as 0:0.01:100. However, some of the entries are not exactly 0.01 apart. In other words, I am trying to understand why the difference for the two following vectors is not zero:
t1 = 0:0.01:100; % original time vector
t2 = round(t*100)./100; % rounded to two decimals
sum(t-a) % difference
ans =
-3.4989e-013
Thanks

 Accepted Answer

Asked and answered many times on this forum. MATLAB uses binary floating point representations of numbers. A decimal number such as 0.01 has no exact representation in this system. E.g., see this link:
Mathematical identities (e.g., multiplying and dividing by the same number) will not always hold true in floating point arithmetic. You simply need to be aware of this and account for it in any code you write.

3 Comments

Thank you for the quick reply. I should have spent more time looking for the answer on past questions. Would you mind suggesting some previous posts for me to read in which this question was answered in more detail?
The post I listed is a good start, and there are several links in it to other posts you can follow as well.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 10 Jun 2015

Commented:

on 10 Jun 2015

Community Treasure Hunt

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

Start Hunting!