Why does MATLAB 7.4 (R2007a) give an erroneous answer using SUM(single(:))?

MATLAB gives erronous answers using the SUM function when the dataset is very large. for example:
n=25e6 ;
a=ones(n,1,'single');
b=sum(a(:))
The value of variable 'b' would be 16777216 which is 32% different from the correct answer of 25e6.
If SUM(SUM(a)) was used the discrepancy would not exist.

 採用された回答

MathWorks Support Team
MathWorks Support Team 2009 年 6 月 27 日

0 投票

The problem starts when too many numbers have been added together. The accumulator in the SUM function gets too big compared to the numbers added. As a result, either some precision is lost in the addition, or the small numbers are completely ignored.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeVehicle Dynamics Blockset についてさらに検索

製品

リリース

R2007a

Community Treasure Hunt

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

Start Hunting!

Translated by