Discrepancies between single and double precision sum over time
3 ビュー (過去 30 日間)
表示 古いコメント
I attempted to isolate and better understand an issue happening in a more complex model. It came down to an internal "clock" we have to count elapsed time. For a sample time of 0.05, the clock implementation would just be a sum coupled with a delay. However, we noticed a considerable cumulative error between single and double precision operations. The order of magnitude of the differences seen in the scope below seems to be way higher than the ones due to the difference in precision between single and double. There's a 1s drift after merely 2100 seconds.
Something else that I am confused about is why the difference seems to shift direction (t≈1000s and t≈4100s). Any insights would be appreciated.


0 件のコメント
採用された回答
Jan
2021 年 12 月 16 日
This is the expected behaviour. Remember that the sum is an instable numerical operation.
d = zeros(1,1e7);
s = zeros(1, 1e7, 'single');
for k = 2:1e7
d(k) = d(k-1) + 0.05;
s(k) = s(k-1) + single(0.05);
end
plot(d - s)
The rounding error accumulate. Single precision means about 7 valid digits. So the magnitude of the rounding effects is in the expected range.
5 件のコメント
Stephen23
2021 年 12 月 17 日
編集済み: Stephen23
2021 年 12 月 17 日
"The part I don't get is why the rounding error does not accumulate monotonically?"
Why should it?
The error (difference between the decimal values that you probably expect vs the actual binary values) is not constant, but depends on both addends... one of which is continuously changing. So the binary amount that you are actually adding changes, because the values that you are adding change.
If there was a simple monotonic linear relationship then all binary floating point error could be compensated for using a trivial offset after any calcuation. But that is definitely not the case.
その他の回答 (0 件)
参考
カテゴリ
Find more on Sources 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!