フィルターのクリア

Problem of Substraction operation on MATLAB R2014a

2 ビュー (過去 30 日間)
Bridget Yu
Bridget Yu 2014 年 12 月 25 日
回答済み: Roger Stafford 2014 年 12 月 25 日
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

回答 (1 件)

Roger Stafford
Roger Stafford 2014 年 12 月 25 日
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.

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by