Why does 7*(0.1) - 0.7 = 1.1102e-16 whereas 9*(0.1)-0.9 =0 in MATLAB?

11 ビュー (過去 30 日間)
swarn singh
swarn singh 2018 年 12 月 5 日
編集済み: Stephen23 2018 年 12 月 5 日
Why does
7*(0.1)-0.7 = 1.1102e-16,
3*(0.1)-0.3 = 5.55112e-17,
6*(0.1)-0.6 = 1.1102e-16
whereas
9*(0.1)-0.9 = 0,
2*(0.1)-0.2 = 0,
4*(0.1)-0.4 = 0 in MATLAB?

回答 (3 件)

Stephen23
Stephen23 2018 年 12 月 5 日
編集済み: Stephen23 2018 年 12 月 5 日
Those values that you are computing with cannot be exactly represented using binary floating point numbers. What you see printed in the command window is the closest representation to 5 or 16 significant digits, depending on your current format setting. To see the "real" value download James Tursa's FEX submission:
Use James Tursa's num2strexact and you will see that none of those values really have the exact value 0.1, or whatever value you use. All you are looking at is a representation of those floating point numbers displayed in the command window, to the precision defined by your format setting. Just because you see 0.1 displayed tells you nothing about the "real" floating point number's value.
Note that you can change how the value is displayed by changing the format.
You need to learn about the limits of floating point numbers. Start by reading these:
This is worth reading as well:

madhan ravi
madhan ravi 2018 年 12 月 5 日

the cyclist
the cyclist 2018 年 12 月 5 日
See the section "Accuracy of Floating-Point Data" in this documentation page.
Some of those combinations are represented by the same floating point value, and some are not.

カテゴリ

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