trying to understand why I get 2.2204e-16 rather than 0

12 ビュー (過去 30 日間)
Ancalagon8
Ancalagon8 2021 年 8 月 28 日
コメント済み: Walter Roberson 2021 年 8 月 28 日
Im trying to understand why when i make x-y (where x=1.6 and y=1.6) i do not get 0 as a result.

回答 (1 件)

DGM
DGM 2021 年 8 月 28 日
編集済み: DGM 2021 年 8 月 28 日
Simply put, the assumption that both x and y are equal to the same value is incorrect. One or both have accumulated rounding error in some undescribed process.
% x and y are assigned the same value
x = 1.6;
y = 1.6;
x-y
ans = 0
% y has accumulated some rounding error
k = pi;
y = k*(y/k);
x-y
ans = 2.2204e-16
% the difference is nonzero, but they look the same
[x y]
ans = 1×2
1.6000 1.6000
% but that's just because the error is small relative to the nominal value
[x y]-1.6
ans = 1×2
1.0e+-15 * 0 -0.2220
Not all numbers can be represented exactly in base 2, just as not all numbers (e.g. 1/3) can be represented exactly in base 10.
  1 件のコメント
Walter Roberson
Walter Roberson 2021 年 8 月 28 日
Minor correction: all rational numbers can be represented exactly in base 2 or base 10 -- but it might take infinite precision to do so!! Since there is only a finite amount of resources in the universe, what we can represent in base 2 or base 10 in a computer is finite length rather than infinite length, not all rational numbers can be represented in base 2 or base 10 in a computer .

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by