MATLAB: Do I get different results for this division

divisionMATLAB

Hello,
I have the following piece of code. I expect to get b = 0:
R1 = 1e3;
Cond = 2e-12;
b = 1/R1/Cond - 1/(R1*Cond); % The result is not zero
But if I try the following, the result is zero:
A = 4;
B = 2;
C = 1/A/B - 1/(A*B);
I thought that the operation 1/a/b was always equal to 1/(a*b). Maybe it's due to the fact that the variable Cond is small. I'd like to know why there are differences.
Thanks.

Best Answer

"I thought that the operation 1/a/b was always equal to 1/(a*b). "
Mathematically they are the same. But for numeric computations they are not at all the same, especially when you are using values large differences in magnitude. Basically you have forgotten to take into account the precision limits of floating-point values, and how floating-point errors propagate through a calculation.
"I expect to get b = 0:"
The values of R1 and Cond differ by fifteen orders, which is right on the limit of double precision. There is no reason to expect the two calculations to produce the same output..
This is a topic that has been discussed many times before:
etc, etc, etc