필터 지우기
필터 지우기

if statement different answer when input variable vs number

조회 수: 1 (최근 30일)
pwprnt
pwprnt 2017년 2월 10일
답변: dpb 2017년 2월 10일
I do not understand why the if statement here produces different results. In one statement, I use the variable names, and in the other I substitute the variable values for the names. Each if statement should yield 'true' because no values are changed.
>> time_end
time_end =
30.0000
>> time_met_end
time_met_end =
30
>> if time_end>time_met_end
disp('true')
end
true
>> if 30.0000 > 30
disp('true')
end
>>
>>

채택된 답변

dpb
dpb 2017년 2월 10일
See the FAQ Why is 0.3-0.2-0.1 not equal to zero? for background in "why". The variable containing what is printed at the command line as 30.0000 isn't quite that, exactly. That the trailing zeros after the decimal are printed is the giveaway that it isn't integer-valued.
Try
time_end-30
to see the remainder for
fprintf('%.15e',time_end)
to display the value with all the digits shown.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by