フィルターのクリア

Rem function yields wrong output.

2 ビュー (過去 30 日間)
BM
BM 2017 年 10 月 23 日
コメント済み: BM 2017 年 10 月 24 日
I am assuming that there is a little error (perhaps floating point), in my test loop. This particular loop I wrote is supposed to ensure that I have integer numbers. For example:
a = 0.07;
T = 150;
Value = 2*a*T
if rem(2*a*T,1) ~= 0
error('2*p*stripsteps must be an integer value.')
end
Result = 2*a*T
which yields:
untitled
Value =
21.0000
Error using untitled (line 9)
2*p*stripsteps must be an integer value.
As one can see, Result does not print, but it should! Value and Result should both print and give me the same value, as they are both the same integer. When I use the rem function; however, I get the value:
rem(2*a*T,1)
ans =
3.5527e-15
But, if I plug in the actual value of 2*a*T, which is 21, as one can see from the initial section of code, I get the correct value:
rem(21,1)
ans =
0
Does anyone have any suggestions on how I can address this issue?

採用された回答

Honglei Chen
Honglei Chen 2017 年 10 月 23 日
編集済み: Honglei Chen 2017 年 10 月 23 日
This is due to the round off error from floating point computation. The following link might be helpful
In general you can try to use a tolerance, for example, instead of using
if rem(2*a*T,1)~=0
Use
temp = 2*a*T;
if abs(round(temp)-temp)>sqrt(eps)
HTH
  1 件のコメント
BM
BM 2017 年 10 月 24 日
Thanks, makes sense. I'll be careful about this in the future.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatched Filter and Ambiguity Function についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by