Clear Filters
Clear Filters

if statement different answer when input variable vs number

1 view (last 30 days)
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
>>
>>

Accepted Answer

dpb
dpb on 10 Feb 2017
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.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!