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 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
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!