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

1 view (last 30 days)
Gergely Bunth
Gergely Bunth on 23 Aug 2019
Commented: Gergely Bunth on 23 Aug 2019
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
Gergely Bunth
Gergely Bunth on 23 Aug 2019
Thank you. Now I remember I should have known this.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!