It looks like you really need to understand floating point numbers.
It looks like 3e-5. But is it? Changing the display format in MATLAb will do a lot, to see something closer to the number stored in MATLAB.
As you can see, the number stored in MATLAB is not exactly 3e-5. But only close. Part of the problem is that MATLAB did not truly use the original numbers.
sprintf('%0.55f',2.6599)
ans = '2.6598999999999999310773546312702819705009460449218750000'
sprintf('%0.55f',2.65987)
ans = '2.6598700000000001786304437700891867280006408691406250000'
Neither number is exactly representable as a double precision number, just like 1/3 or 2/3 are not representable as a decimal number in a finite number of digits. So the difference of those two numbers will also be something that is not exactly what you think.
Numbers in MATLAB (at least those represented as doubles) are stored using a binary representation based on 52 bits of precision. And almost all decimal fractions cannot be represented in a finite number of binary bits.
NEVER trust the least significant digits of a floating point number, at least, not until you understand what numbers can be represented exactly, and the storage methods employed to encode them in MATLAB.
Of course, you could have used symbolic arithmetic to perform those computations in a better way.
a=abs(sym('2.6599')-sym('2.65987'))
Done in a symbolic form here, where the numbers are stored in an exact decimal form, MATLAB was albe to return the exact result. But be careful, as the next computation is very different.
a=vpa(abs(sym(2.6599)-sym(2.65987)))
a = 0.000029999999999821369556229910813272
The difference arises because the numbers are passed to sym as doubles, so they are not exactly stored.
So part of your problem lies in not understanding floating point numbers. But the rest of it, to see more digits of the number can be done either with the format command, or by a better use of tools like fprintf or sprintf. And for that, you would be best served just to read the help docs for sprintf and fprintf.
Best Answer