small floating point has been considred as zero value!
3 ビュー (過去 30 日間)
古いコメントを表示
hi,
x=0.16
if x>0.1
d=1
else
d=0
end
the answer is 0
I do not know where is the problem?
1 件のコメント
Les Beckham
2024 年 3 月 1 日
I don't see a problem. Are you sure this is really the code that you are having the problem with?
x = 0.16
if x > 0.1
d = 1
else
d = 0
end
回答 (2 件)
Steven Lord
2024 年 3 月 1 日
More likely than not, your value for x is not what you think it is. If we run your code on Answers:
x=0.16
if x>0.1
d=1
else
d=0
end
Now if your number was part of a larger array with numbers of varying magnitudes, part of the display may look like 0.16 without it actually being 0.16:
x = [1e-6 0.16e-3]
Note that x(2) displays as 0.16 but there's that "1.0e-03 *" right below the name of the variable. So it's actually much smaller; basically one one-thousandth of 0.16:
y = x(2)
1000*y-0.16
and that is not greater than 0.1 (aka 1.0000e-01)
format shorte
z = 0.1
So when we use y in place of 0.16, MATLAB correctly identifies that y is not greater than 0.1:
if y > 0.1
d = 1
else
d = 0
end
0 件のコメント
Star Strider
2024 年 3 月 1 日
Your code works correctly when I run it here —
x=0.16
if x>0.1
d=1
else
d=0
end
d
.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!