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.

回答(1 个)

James Tursa
James Tursa 2020-11-10
编辑:James Tursa 2020-11-10
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.

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by