I am trying to perform the vector sum below. Why am I getting 0.0000 in the 3rd entry when I should be getting 0

2 views (last 30 days)
[-1/14 -1/7 -3/14 0] - 1/5*[9/14 9/7 -15/14 0]
= -0.2000 -0.4000 0.0000 0
Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Jan 2017
The fractions 1/5, 1/7 and 1/14 are not exactly representable in finite binary floating point, just the same way that 1/7 and 1/14 cannot be exactly represented in finite decimal expansion.
It is much the same problem as representing 1/3 as 0.33 . Add that together three times and you get 0.99 instead of 1, as the truncation losses build up.
  6 Comments
Eddie
Eddie on 28 Jan 2017
Wow thank you so much for the input. Nope I did not initialize i and j, and only because I didn't see a need to, and now I do. I'll make the changes you recommended, take care.
Walter Roberson
Walter Roberson on 29 Jan 2017
Your "for" loops are initializing i and j okay; I was discussing in general how it can be hard in code to notice that you did not initialize variables. Failure to initialize becomes more obvious if you are using a variable name that is not i or j or pi (or inf or NaN or nan !), but if you happen to use one of those as a variable name then you can end up staring at the code for a long time trying to figure out what is going wrong. So good coding practice is to avoid using any of those as variable names.

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!