MATLAB: 6*0.003125 – 0.018750 = 0, right? How to get the right answer

06*0.0031256*0.003125 - 0.018750is not zeronumerical error

In Matlab >> 6*0.003125
ans =
0.018750000000000
>> 6*0.003125 – 0.01875
ans =
3.469446951953614e-18
In Mathematica
In[17]:= 6*0.003125 6*0.003125 – 0.018750
Out[17]= 0.01875
Out[18]= 3.46945*10^-18
~~~~~~~~~~~~~~~~~~~~~~~~~~~How can I get the right answer??

Best Answer

Matlab is giving you the right answer. Matlab uses IEEE 754 binary floating point double precision. Floating point operations are not algebraic and not generally commutative.
In binary floating point there is no exact representation of 0.1, just the same way that in base 10 there is no way to exactly represent 1/7 in any finite precision decimal number.