필터 지우기
필터 지우기

small floating point has been considred as zero value!

조회 수: 3 (최근 30일)
juvanni
juvanni 2024년 3월 1일
답변: Steven Lord 2024년 3월 1일
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
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
x = 0.1600
if x > 0.1
d = 1
else
d = 0
end
d = 1

댓글을 달려면 로그인하십시오.

답변 (2개)

Steven Lord
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
x = 0.1600
if x>0.1
d=1
else
d=0
end
d = 1
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]
x = 1x2
1.0e-03 * 0.0010 0.1600
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)
y = 1.6000e-04
1000*y-0.16
ans = 0
and that is not greater than 0.1 (aka 1.0000e-01)
format shorte
z = 0.1
z =
1.0000e-01
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
d =
0

Star Strider
Star Strider 2024년 3월 1일
Your code works correctly when I run it here —
x=0.16
x = 0.1600
if x>0.1
d=1
else
d=0
end
d = 1
d
d = 1
.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by