Problem of Substraction operation on MATLAB R2014a
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Please find in the attached photo.
This is an example of basic calculation. 912.7400 - 912.7400 should be 0, however MATLAB returns -7.9581e-13. Please let me know how to resolve it. Thanks.
>> balance_201402 + (charged_201403 + fine_201403)
ans =
912.7400
>> (paid_201403 + refund_201403)
ans =
912.7400
>> balance_201402 + (charged_201403 + fine_201403) - (paid_201403 + refund_201403)
ans =
-7.9581e-13
Respuestas (1)
Roger Stafford
el 25 de Dic. de 2014
Just because the two quantities both displayed 912.7400 does not mean that the quantities they represent in the computer are actually equal. You are using the default "format short" which rounds displayed numbers to four decimal places, so they could easily be different to the right of the fourth place. If you want to display numbers exactly, you can use "format hex" which gives a hexadecimal representation of the actual bits stored in the computer. If those are the same for two different variables, then you are guaranteed that their difference will be an exact zero.
Also, as Per indicates, always bear in mind that your computer is using binary representation, not decimal, so it cannot exactly represent a decimal fraction such as 912.7400, 0.1, or the like. These would usually be off by a tiny amount in the bits beyond the 53-rd significant bit. Only if it is a fraction whose denominator is a power of 2 such as 41.125 (which is 329/2^3) can it be represented exactly.
0 comentarios
Ver también
Categorías
Más información sobre Numeric Types en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!