Number of precision digits( sum(sum(A)) not equal to sum(A:)

2 ビュー (過去 30 日間)
Monkeymengmeng
Monkeymengmeng 2018 年 6 月 25 日
コメント済み: Monkeymengmeng 2018 年 6 月 26 日
Hey guys. I am new to Matlab. And I need to compute a two-dimension matrix(65 x 65). called matrix A. sth like:
-0.043209482621010 -0.043209460507728 -0.043209425775628 -0.043209372126255
-0.043209488597448 -0.043209470284528 -0.043209441521445 -0.043209397092189
-0.043209517396291 -0.043209517396291 -0.043209517396291 -0.043209517396291
-0.043209590936714 -0.043209637700394 -0.043209711149500 -0.043209824603580
....
But when I do the operation
sum(sum(A)) it computes:1.847411112976260e-13
and the operation
sum(A(:)) it computes:5.652422974122828e-14
shouldn't they be the same consequence? I am confusing with that. Maybe it is about the precision digits. Can anyone help me?Thanks so much.
  2 件のコメント
Stephen23
Stephen23 2018 年 6 月 25 日
This is just accumulated floating point error, and is easy to demonstrate:
>> M = [+1e7,-1e7;1e-7,-1e-7]
M =
1.0000e+007 -1.0000e+007
1.0000e-007 -1.0000e-007
>> sum(sum(M))
ans = 0
>> sum(M(:))
ans = 0.00000000058284
This is exactly the same as every other question asked on this forum regarding floating point numbers: different operations on floating point numbers can (and usually do) produce different output values. Summing some values, then summing those sums is clearly a different set of operations than rearranging and then summing.
Monkeymengmeng
Monkeymengmeng 2018 年 6 月 26 日
thanks so much.Stephen.

サインインしてコメントする。

採用された回答

Ameer Hamza
Ameer Hamza 2018 年 6 月 25 日
編集済み: Ameer Hamza 2018 年 6 月 25 日
It might be happening because of finite precision of floating point numbers, Also, calling the sum() twice seems to be amplifying the errors caused by the finite precision. Since you are dealing with very small numbers, you might want to use arbitrary precision arithmetic to get accurate answers.

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 6 月 25 日

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by