sum results depending on array orientation
2 次查看(过去 30 天)
显示 更早的评论
To me the flaw is serious enough that it needs attention so I create a separate thread.
As you see in this demo code the sum of an array depends how the array is oriented, and one of the result is way off when n is large
n = 2^25;
x =single(128);
A = x+zeros(2,n,'single');
B = A.';
sAt = sum(B,1).' % correct sum result
s = x*n % expected value
sAt == s % fine
sA = sum(A,2) % big roundoff error
sA == s
4 个评论
Matt J
2024-1-16
编辑:Matt J
2024-1-16
I think it's also instructive to show how breaking up the sum(A,2) into smaller block sums can mitigate the issue,
n = 2^25;
x =single(128);
A = x+zeros(2,n,'single');
Ar=reshape(A,2,2^10,[]);
sum(A,2) %running sum
sum(sum( Ar , 2) ,3) %sum of partial sums
However,
sum(Ar,[2,3]) %running sum!
Xiaoxing Zhang
2024-6-6
移动:Matt J
2024-6-6
In my case
a=rand(10000,1);
sum(a)==sum(sort(a))
always returns false. I opend a ticket and the replay basically says these are expected round-off errors and one should use
(sum(a)-sum(sort(a)))<(10*eps)
instead.
回答(2 个)
Matt J
2024-1-16
编辑:Matt J
2024-1-16
Now the inconsistent results is considered by TMW as a bug
Here is what they said,
After some investigation, I found out that the inconsistency you are seeing is actually a bug inside MATLAB, and I sincerely apologize for any trouble this bug has caused you....
Right now, the issue is that MATLAB loses a lot of precision, and we believe the intended behavior is "mean()" [Ed. sum()] returning the answer much closer to ...the correct result under this case.
Notice that they say that the answer should be much closer to correct, but do not committ that it should be precisely correct, even though it is an integer-valued operation. So, I assume that the intended behavior is only to reduce the chances of precision underflow, but to do so equally along all dimensions.
Hassaan
2024-1-16
编辑:Hassaan
2024-1-16
This issue is related to the limitations of floating-point precision and the way MATLAB handles summation along different dimensions. It is commonly known as the "loss of precision" problem when summing floating-point numbers.
When you sum along different dimensions, especially when dealing with large arrays, the order of summation can affect the result due to the limited precision of floating-point arithmetic. This is not specific to MATLAB but is a general concern in numerical computing.
In the case of your code, the issue arises when summing along the second dimension (sA = sum(A,2)). The large number of elements in the array and the order of summation can lead to significant roundoff errors.
Here's a modified version of your code with additional explanations:
n = 2^25;
x = single(128);
A = x + zeros(2, n, 'single');
B = A.';
sAt = sum(B, 1).'; % Sum along the first dimension
s = x * n;
% Check if the sum along the first dimension is close to the expected value
disp('Check for sum along the first dimension:');
disp(all(abs(sAt - s) < 1e-6)); % You might need to adjust the tolerance
sA = sum(A, 2); % Sum along the second dimension
disp('Check for sum along the second dimension:');
disp(all(abs(sA - s) < 1e-6)); % You might need to adjust the tolerance
Note:
The all(abs(sAt - s) < 1e-6) and all(abs(sA - s) < 1e-6) checks if the computed sum is close enough to the expected value, considering a tolerance (1e-6 in this case). You may need to adjust the tolerance based on your specific requirements.
In summary, this behavior is a consequence of the limited precision of floating-point arithmetic. It's important to be aware of such issues and carefully choose the order of operations when dealing with large arrays and floating-point numbers.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!