sum(c*b,'a​ll')>sum(c​)*sum(b)

b=[0.4 0.4 0.1 0.1]
c=[0.6 0.4]'
sum(c*b,'all')>sum(c)*sum(b)
returns 1.
sum(c)=sum(b)=1, while sum(c*b,'all') seems to be larger than 1. Why is that?

 Accepted Answer

Matt J
Matt J on 23 Aug 2019
Edited: Matt J on 23 Aug 2019
Floating point discrepancies. You cannot count on floating point calculations giving a perfectly accurate result of 1 (unless they involve only integer arithmetic).
>> x=c*b; sum(x(:))-1
ans =
2.220446049250313e-16

1 Comment

Thank you. Now I remember I should have known this.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2018b

Community Treasure Hunt

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

Start Hunting!