if statement different answer when input variable vs number
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
>>
>>
0 comentarios
Respuesta aceptada
dpb
el 10 de Feb. de 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.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!