min function why do results change for numbers with decimal values?
Mostrar comentarios más antiguos
ex1:
X=[1:11];
Y=double( 1.0*X);
diffY = diff(Y);
[diffY_min,diffY_index] = min (diffY);
ans diffY_min = 1 diffY_index=1
ex2:
X=[1:1.4:15];
Y=double( 1.0*X);
diffY = diff(Y);
[diffY_min,diffY_index] = min (diffY);
ans diffY_min = 1.4 diffY_index=8
all numbers are double prec. Why does min treat numbers N.xxx different than N? Get similar results if I change slope fm 1 to 2.1 or sqrt(2) or any value that results in diffY .NE. 1,2,3....
1 comentario
SteveH
el 9 de Dic. de 2015
Respuestas (2)
Stephen23
el 9 de Dic. de 2015
When you compare floating point numbers (such as using == or unique) you should use a tolerance value like this:
>> tol = 1e-3;
>> abs((0.05+0.01) - 0.06) <= tol
ans =
1
And you should read about floating point numbers to know why this is required:
And some external links on this topic:
1 comentario
SteveH
el 9 de Dic. de 2015
Categorías
Más información sobre Numeric Types en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!