フィルターのクリア

Why isn't the numerical value assigned by matlab verified using == function.

40 ビュー (過去 30 日間)
Yogesh
Yogesh 2024 年 8 月 8 日 4:15
コメント済み: Yogesh 2024 年 8 月 8 日 7:37
I have calculated the value for R using my code which has assigned 0.0094 but if I try to verify using == function , it shows logical zero.
I had used format short but evidently isn't doing much.
what should I change here.
  1 件のコメント
Stephen23
Stephen23 2024 年 8 月 8 日 4:37
編集済み: Stephen23 2024 年 8 月 8 日 4:58
"I had used format short but evidently isn't doing much."
FORMAT changes how numeric data are displayed, just as the FORMAT documentation explains.
FORMAT does not change what data are stored in memory. What numeric data are stored in memeory and how they are displayed are two different things: mixing them up like you are doing will not make working with numeric data easy.
"what should I change here."
Either use ISMEMBERTOL or compare the absolute difference against some tolerance (of your choice):
abs(R-0.0094)<0.00005

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

採用された回答

Steven Lord
Steven Lord 2024 年 8 月 8 日 4:30
This behavior is a consequence of floating point arithmetic. See this Answers post and the "Avoiding Common Problems with Floating-Point Arithmetic" section of this documentation page for more information.
If you are using the == operator to attempt to locate a floating-point number in an array, instead subtract the number you're trying to find from the numbers in the array and locate those positions where the difference is smaller than some tolerance or use the ismembertol function. For example:
x = 0:0.1:1
x = 1x11
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
It appears that x contains the value 0.3, but it does not contain exactly 0.3.
checkWithExactEquality = x == 0.3
checkWithExactEquality = 1x11 logical array
0 0 0 0 0 0 0 0 0 0 0
It does contain a value that is extremely close to 0.3, however.
tolerance = 1e-15;
checkWithTolerance = abs(x-0.3) < tolerance
checkWithTolerance = 1x11 logical array
0 0 0 1 0 0 0 0 0 0 0
whichValueTolerance = x(checkWithTolerance)
whichValueTolerance = 0.3000
How far away from 0.3 is the value we found using a tolerance?
howDifferent = whichValueTolerance - 0.3
howDifferent = 5.5511e-17
To do the same with ismembertol:
checkWithIsmembertol = ismembertol(x, 0.3, tolerance)
checkWithIsmembertol = 1x11 logical array
0 0 0 1 0 0 0 0 0 0 0
whichValueIsmembertol = x(checkWithIsmembertol)
whichValueIsmembertol = 0.3000
The ismembertol function found the same value that the check with a tolerance did.
Don't confuse how a number is displayed with how it is stored in memory.

その他の回答 (1 件)

Divyajyoti Nayak
Divyajyoti Nayak 2024 年 8 月 8 日 4:25
編集済み: Divyajyoti Nayak 2024 年 8 月 8 日 4:31
Hi @Yogesh, the 'format' command does not change the precision of your variables. It only changes the format in which the output is displayed. The actual value of R is unchanged.

カテゴリ

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