Why is (2.24*10)-22.4 not equal to 0?
77 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
James Tursa
on 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
0 Comments
More Answers (2)
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.
3 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!