logical less than thinks values are the same?

I have wracked my brain over this one. It must be a dumb mistake.
if scalarMin < 1.0
fprintf('*scalarMin %f.\n',scalarMin);
end
scalarMin is exactly 1.000000, but it insists on printing anyway. Just want an output when it's actually <1.
What could it be?

4 comentarios

What is the output of
num2hex(scalarMin)
?
Well, as you're going to find out when answer Walter's question, it's not < that's the problem; scalarMin actually is going to be somewhat less than 1 in memory; with default format in command window if it were actually integer one the printed value would show as an integer --
>> format short
>> scalarMin=1 % make it exactly 1; shows up as "1"
scalarMin =
1
>> scalarMin=scalarMin-2*eps(1) % make it just a tad less...
scalarMin =
1.0000
>>
Note the difference.
Don't trust comparisons to floating point as being exact; even < can lead to unexpected results if not wary.
format long g
x = 1 - eps
x =
1
It sure looks like 1. But is it?
sprintf('%0.55f',x)
ans =
'0.9999999999999997779553950749686919152736663818359375000'
No, it is not 1. And MATLAB confirms that fact.
x < 1
ans =
logical
1
David Pesetsky
David Pesetsky el 31 de Mzo. de 2018
num2hex gives
3fefffffffffffff
What's that?

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 31 de Mzo. de 2018
Editada: John D'Errico el 31 de Mzo. de 2018
You don't seem to believe what we are telling you. Here is 1.
num2hex(1)
ans =
'3ff0000000000000'
That is 1. Creating something slightly smaller than 1? Try this:
num2hex(1-eps)
ans =
'3feffffffffffffe'
Just because a number displays at the command line as looking like 1, it need not be exactly 1.

4 comentarios

>> fprintf('%.99g\n', 1 - hex2num('3fefffffffffffff'))
1.1102230246251565404236316680908203125e-16
David Pesetsky
David Pesetsky el 1 de Abr. de 2018
I'll compare to 0.99999999 instead.
That works fine.
John D'Errico
John D'Errico el 1 de Abr. de 2018
Editada: John D'Errico el 1 de Abr. de 2018
Just a teeny tiny, itsy bitsy amount less than 1. But not 1.
sprintf('%0.55f',hex2num('3fefffffffffffff'))
ans =
'0.9999999999999998889776975374843459576368331909179687500'
As much as it looks like 1,
format long g
hex2num('3fefffffffffffff')
ans =
1
What is the saying? Close is only good in horseshoes and hand grenades.
Walter Roberson
Walter Roberson el 1 de Abr. de 2018
Consider using ismembertol()

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 31 de Mzo. de 2018

Comentada:

el 1 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by