フィルターのクリア

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

1 回表示 (過去 30 日間)
[-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.

採用された回答

Walter Roberson
Walter Roberson 2017 年 1 月 21 日
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 件のコメント
Eddie
Eddie 2017 年 1 月 28 日
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 2017 年 1 月 29 日
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.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by