Why is (2.24*10)-22.4 not equal to 0?

31 Ansichten (letzte 30 Tage)
Morten
Morten am 19 Jun. 2012
I need to make some frequency responses where I made an frequency array according to the preferred numbers R40, fR40. Later I need to find the indices in another array which corresponds to the frequencies in fR40. The problem is that MatLab returns an empty array when I use find. I find out that the problem lies with some of numbers from the R40. Fx 2.12 and 2.24.
When typing (2.24*10)-22.4 in the command window yields 3.5527e-015.
Adding one decimal more fx 2.241 and 22.41 yields 0 as expected.
Can someone explain to me why it is that this happens?

Akzeptierte Antwort

James Tursa
James Tursa am 21 Jun. 2012
>> num2strexact(0.3)
ans =
0.299999999999999988897769753748434595763683319091796875
>> num2strexact(0.2)
ans =
0.200000000000000011102230246251565404236316680908203125
>> num2strexact(0.1)
ans =
0.1000000000000000055511151231257827021181583404541015625
>> num2strexact(0.3-0.2-0.1)
ans =
-2.77555756156289135105907917022705078125e-17

Weitere Antworten (2)

the cyclist
the cyclist am 19 Jun. 2012

Jan
Jan am 20 Jun. 2012
This is a frequently asked question. All frequently asked questions appear frequently in this and other forums. They contain problems, which occur usually for nearly all beginners. Therefor it is recommended to study the FAQ for all software packages you use. In the case of Matlab, you find them here: http://matlab.wikia.com/wiki/FAQ, and for your question Why is 0.3 - 0.2 - 0.1 ~= 0 .
This message is implicitely given in the answer of the cyclist. Anyhow, I could not resist to suggest reading the complete FAQ. Actually I'd prefer to add a checkmark in the login dialog of this forum, that the FAQ are read before a question is asked, because this would reduce the number of repeated questions.
  3 Kommentare
Walter Roberson
Walter Roberson am 20 Jun. 2012
IEEE 754 has added a standard for computation with decimal numbers. The only commercial manufacturer that I have heard of whom has implemented it on general purpose machines (instead of DSPs) is IBM, in a few of the models in their Z series machines. I don't think it has been implemented on any x86 or x64 architecture.
The great majority of programming environments have this issue, and it can be easily proven that the issue exists in all possible floating point or fixed point machines using any finite base numbers -- base 2, base 10, base 16, base 3, base 60, systems in which each digit is allowed to belong to any base from 1 to 1000 -- *every* fixed base machine. I'm pretty sure that the problem exists even if you do arithmetic in base Pi or exp(1) or sqrt(2) or the Golden Ratio or any other infinitely-long number base that uses any finite number of digits -- but proving that is not nearly as easy.
MATLAB users who have access to the (software implemented) Symbolic Toolbox can do arithmetic in base 10.
Jan
Jan am 21 Jun. 2012
Let me vote for Walter's survey.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by