MATLAB: Is MATLAB messing up this very simple calculation.

floating point error.floating point numbernot a bugright valuewrong value

I was trying to tutor MATLAB to one of the underclassman I tutor. I came across an error that I initially could not fix. I eventually found the error but it isn't my error. MATLAB calculated the wrong number! -2.9982*5=-14.991 not -2.9982*5=-14.991000000000001. I can avoid using this calculation in the code but I just want to know what is causing this silly error so I can avoid it again.

Best Answer

"MATLAB calculated the wrong number!"
Nope, MATLAB calculated the right number.
The reason is simply that the value -2.9982 cannot be represented exactly using binary floating point numbers, so any calculation involving that number will inevitably accumulate some floating point error.
In exactly the same way that you cannot write 1/3 exactly using a finite decimal fraction, it is impossible to store -2.9982 exactly using a finite binary floating point number. So although you might think that you have -2.9982, in fact the real values stored in computer memory is slightly different from this. And so when you perform some calculation on it, the calculation collects this floating point error, so that the final total is not going to be exactly equal to -14.991 (unless your calculation involves only powers of two).
What you see printed in the command window is the closest representation to 5 or 16 significant digits, depending on your current format setting. To see the "real" value download James Tursa's FEX submission:
Use James Tursa's num2strexact and you will see that that number does not really have the exact value -2.9982. All you are looking at is a representation of those floating point numbers displayed in the command window, to the precision defined by your format setting. Just because you see -2.9982 displayed in the command window tells you nothing about the real floating point number's value.
Note that you can change how the value is displayed by changing the format.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well: