Why summing a matrix in 2 different ways can give not the same numbers ??? Does anyone know?

2 views (last 30 days)
I had a big problem with a code because of this mistake which looks silly but maybe it's not..
suppose
f1 = .4 ; f2 =.5199 ; f4 = 0.0267 ; f3 = 0.0534 ; Total_mass = 4.799844e+22; frac_mass = [f1,f2,f3,f4]*Total_mass ;
sum(frac_mass) == sum(frac_mass(4:-1:1)) gives me '0' (FALSE) as an answer.. if you check both numbers they are the same... but setting "format long" there's a factor 1 after some zeros when you check the right-hand statement above. Where does this "1" come from? I really thought the results should've been the same.. Need some help on it please..

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 23 May 2014
Edited: Azzi Abdelmalek on 23 May 2014
Because the summations are done in different order, look at this link http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F
Note that the results are slightly different

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!