MATLAB: VERY odd error with simple multiplication … OK… not so odd.

floating pointnumeric errorprecision errorvariable class

Hi all, I'm encountering a strange error with the following bit of code. All of the variables are double precision.
preWinT = .20;
info.micFsr2 =2000;
T360_wrong = (preWinT-.02)*info.micFsr2
info.micFsr3 =20000;
T3600_correct = (preWinT-.02)*info.micFsr3
Notice the trailing "1" in T360_wrong in the output that follows:
T360_wrong = 3.600000000000001e+02
When increasing the multiplier from 2000 to 20000, the error does not appear.
T3600_correct = 3.600000000000000e+03
This is even happening when entering purely numeric values in the command window.
(.2-.02)*2000
ans = 3.600000000000001e+02
The error again disappears when I enter
(.18)*2000
ans = 360
Does anyone have any idea why this is happening? It's pretty disturbing to think about the ramifications of this kind of error.
Thanks,
David

Best Answer

"Does anyone have any idea why this is happening?"
Yes, the behaviors of floating point numbers are quite well documented, and often discussed on this forum.
"It's pretty disturbing to think about the ramifications of this kind of error."
Only if you write your code believing that floating point error does not exist. Experienced programmers understand that calculations on floating point numbers can accumulate floating point errors (like your example does), and do not expect floating point mathematics to be equivalent to the symbolic maths that they learned in high school (i.e. arithmetic operations on floating point values are not associative), so they write their code accordingly to allow for this.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well: