Floating point accurarcy and COLON
6 views (last 30 days)
I have just been bitten by an odd floating point comparison issue. At the heart of the issue is that I expected to get true for the following test
x = 0:0.1:0.3;
y = 0:0.1:0.4;
x - y(1:end-1)
0 0 -0.2776 -0.5551
The help for COLON says J:D:K is the same as [J, J+D, ..., J+m*D] where m = fix((K-J)/D) which I think supports my expectation. It is not quite right since for 0:0.1:0.3, fix((K-J)/D) is 2. I thought possibly COLON was just doing a linspace, but I also get false for
z = linspace(0, 0.3, 4);
Interestingly, for all the cases I have tried I get true for
a = 0;
b = 0.3;
c = 0.1;
x = a:c:b;
suggesting COLON somehow fixes the ends, but not in the same way that LINSPACE does.
How does COLON work?
Matt Fig on 16 Nov 2012
This explains how the colon operator works. It is an interesting read.
Here is a relevant portion:
"To counteract such error accumulation, the algorithm of the COLON operator dictates that:
1. The first half of the output vector (in this example ‘v’) is calculated by adding integer multiples of the step ‘d’ to the left-hand endpoint ‘a’. 2. The second half is calculated by subtracting multiples of the step ‘d’ from the right-hand endpoint."