Why do I get empty double vector

I'm a new user of Matlab and I have met a weird problem. I tried to create a simple function like this:
y=0:0.0025:2.5;
x=2*y;
disp(x(y==1.15));
and I will get a 1x0 empty double row vector instead of 2.3. When I tried y==1.12 or y==1.16, I could get the correct result. I suppose that might because of the double integer but I don't really understand.

Respuestas (1)

James Tursa
James Tursa el 10 de Nov. de 2020
Editada: James Tursa el 10 de Nov. de 2020

0 votos

This is normal floating point arithmetic behavior. None of 0.0025 or 1.15 or 1.12 or 1.16 can be represented in IEEE double precision exactly, so you will get small differences showing up when compared to exact decimal representations in calculations, and these differences can propagate through further calculations. If this makes a difference to your algorithm, you will need to rewrite your algorithm to account for these effects. E.g., using tolerances when making comparisons.

Categorías

Productos

Etiquetas

Preguntada:

el 10 de Nov. de 2020

Editada:

el 10 de Nov. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by