Why the result of (1-0.6667)==0.3333 is zero in MATLAB? how can I get one?

2 ビュー (過去 30 日間)
may
may 2013 年 2 月 26 日
Why the result of (1-0.6667)==0.3333 is zero in MATLAB? how can I get one?
when I use matlab to calculate 1-0.6667 I get 0.3333. but the result of (1-0.6667)==0.3333 is zero I even tried this one
t=1-0.6667;
t==0.3333
the result is again 0.
how can I get one for (1-0.6667)==0.3333?

回答 (6 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 26 日
編集済み: Azzi Abdelmalek 2013 年 2 月 26 日
Because 0.6667 and 0.3333 are codified and stored in the memory with a certain precision
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 26 日
編集済み: Azzi Abdelmalek 2013 年 2 月 26 日
use this
tolerence=0.0001
abs((1-0.6667)-0.3333)<tolerence

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


per isakson
per isakson 2013 年 2 月 26 日
編集済み: per isakson 2013 年 2 月 26 日

Shashank Prasanna
Shashank Prasanna 2013 年 2 月 26 日
That is because they are not the same.
>> t-0.3333
ans =
5.55111512312578e-17
They can be equal by some tolerance.
>> t-0.3333 < 1e-15
ans =
1

Youssef  Khmou
Youssef Khmou 2013 年 2 月 26 日
hi may,
This precision problem, so your numbers are 4 decimals , i suggest to make to make your logical test by multiplying your numbers by 1E+4 :
a=0.6667;
b=0.3333;
f=1e+4; % Factor
f-f*a==f*b % ITS NOT f*(1-a)==f*b

Jan
Jan 2013 年 2 月 27 日
This is the most frequently asked question. See FAQ: why is 0.3-0.2~=0.1

Matt J
Matt J 2013 年 2 月 26 日
編集済み: Matt J 2013 年 2 月 26 日
Why the result of (1-0.6667)==0.3333 is zero in MATLAB? how can I get one?
The others have told you why. You can get "one", however, by reformulating this way,
>> (3-2)/3==1/3
ans =
1

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by