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 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Eddie
il 21 Gen 2017
Commentato: Walter Roberson
il 29 Gen 2017
[-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 Commenti
Risposta accettata
Walter Roberson
il 21 Gen 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 Commenti
Walter Roberson
il 29 Gen 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.
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!