Discrepancies between single and double precision sum over time
3 views (last 30 days)
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.
Jan on 16 Dec 2021
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);
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.