6*0.003125 - 0.018750 = 0, right? How to get the right answer?

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??

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 15 Okt. 2015

1 Stimme

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.

Weitere Antworten (2)

Thorsten
Thorsten am 15 Okt. 2015
You can compare the results against the machine precision eps and regard them as equal if smaller than eps
6*0.003125 - 0.01875 < eps

1 Kommentar

I finally understand the equal in case using computer, Thank you.

Melden Sie sich an, um zu kommentieren.

Steven Lord
Steven Lord am 15 Okt. 2015

1 Stimme

For a more in-depth answer to this general situation, with many references, see question 1 in the Mathematics section of the FAQ.

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by