Unexpected Results due to Floating Point Rounding Errors by performing Arithmetic calculations on large numerical values
Show older comments
Why am I getting unexpected results while working with functions such as 'mean' for array containing large numerical values?
Input these commands in the MATLAB 2019a
>> test = [2^1000, 2^1000];
>> testMean = mean(test);
Now, if testMean is actually the mean of test, then subtracting it from test and taking the mean a subsequent time, should result in 0.
>> zeroCenteredTest = test-testMean;
>> zeroCenteredTestMean = mean(zeroCenteredTest);
You will find zeroCenterTestMean is in fact 0. This case works correctly. However, let me demonstrate a case that does not work correctly.
Let me generate a large 1D matrix with a large amount of large values
>> test2 = [];
>> for i=1:1000
test2(i) = 2^i;
end
>> test2Mean = mean(test2);
If this mean was calculated correctly, the result should be 0.
>> zeroCenteredTest2 = test2-test2Mean;
>> zeroCenteredTest2Mean = mean(zeroCenteredTest2);
You will find in this case, the zeroCenteredTest2Mean is -1.66545893749512e+283 which is definitely not or even close to 0.
Accepted Answer
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!