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

5 views (last 30 days)
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

James Tursa
James Tursa on 10 Jun 2015
Edited: James Tursa on 10 Jun 2015
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
James Tursa
James Tursa on 10 Jun 2015
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

Community Treasure Hunt

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

Start Hunting!