MATLAB: What is the smallest difference MATLAB can notice between numbers

computingdifferenceepsilon

If eps is the machine precision and the smallest number such that MATLAB can tell x+eps≠x, when comparing 2 numbers, such as in the loop {while a<b}, what is the smallest difference between a and b such that MATLAB can correctly decide whether a<b? Is it eps or does it change with the sizes of a and b? Thanks 🙂

Best Answer

eps is indeed the smallest difference. However, eps is a FUNCTION, not a fixed constant. The value of eps differs for different numbers.
eps([1 10 1000])
ans =
2.22044604925031e-16 1.77635683940025e-15 1.13686837721616e-13
eps, used on its own, returns eps(1) by default.
eps
ans =
2.22044604925031e-16
The smallest difference that could be detected should be eps(min(abs([a,b]))).
x = 12345;
eps(x)
ans =
1.81898940354586e-12
x < (x + eps(x))
ans =
logical
0
x < (x + eps(x)/2)
ans =
logical
1
Actually, to be a computational pedant, the very smallest difference that can be distinguished should be seen here:
x < (x + eps(x)*(0.5 + eps(0.5)))
ans =
logical
1
For any smaller value of the perturbation, the sum will be indistinct. So while eps(x) is the size of the least significant bit of x, the smallest number that can be added to x and generate a distinct result is smaller than eps(x), because the result gets rounded up to the next larger bit in that case.
So the construction that I show is not truly a significant one, in that we did not create a second number there that was different by less than epx(x).
eps(x)
ans =
1.81898940354586e-12
(x + eps(x)*(0.5 + eps(0.5))) - x
ans =
1.81898940354586e-12
So the smallest difference that can be detected is eps(x). The smallest number that I can add to x and return a different result is eps(x)*(0.5+eps(0.5)).
The distinction is subtle and tiny, but important to understand.
eps(x)*(0.5 + eps(0.5))
ans =
9.09494701772928e-13
eps(x)*(0.5)
ans =
9.09494701772928e-13
eps(x)*(0.5 + eps(0.5)) == eps(x)*(0.5)
ans =
logical
0