Asked by Morten
on 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?

Answer by James Tursa
on 21 Jun 2012

Accepted Answer

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

Sign in to comment.

Answer by the cyclist
on 19 Jun 2012

Take a look at this thread, which answers that:

Sign in to comment.

Answer by Jan
on 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.

the cyclist
on 20 Jun 2012

Lucas, when you say that those numbers are not repeating, you are considering them as decimal representations, not binary representations. (For example, 0.1 decimal requires an infinite series to be exactly represented in binary.) You can learn more by reading this: http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf

It is most definitely not a bug.

Walter Roberson
on 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
on 21 Jun 2012

Let me vote for Walter's survey.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.