is this a bug?

I try to write a very simple program like this
function [ey]=check()
ex=2;
F=[[0 0 0.26 1.3];[0.6 0 0.86 1.3];[1.2 0 1.46 2.6];...
[1.8 0 2.06 2.6];[2.4 0 2.66 2.6]]; % [x1 y1 x2 y2 Mat q]
fanzahl=length(F(:,1)); % 5
X=abs(F(1:fanzahl,3)-F(1:fanzahl,1));% x-length of every F
Y=abs(F(1:fanzahl,2)-F(1:fanzahl,4));% y-length of every F
ey=abs(ex*Y./X);
for i=1:fanzahl
p(ey(i));
end
end
function []=p(ny)
ny
0:ny
end
and I want to output the ny and 0:ny in the subroutine. after calculation I get this.
ny =
10
ans =
0 1 2 3 4 5 6 7 8 9 10
ny =
10
ans =
0 1 2 3 4 5 6 7 8 9 10
ny =
20
ans =
Columns 1 through 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Columns 15 through 21
14 15 16 17 18 19 20
ny =
20
ans =
Columns 1 through 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Columns 15 through 21
14 15 16 17 18 19 20
ny =
20.0000
ans =
Columns 1 through 14
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Columns 15 through 20
14 15 16 17 18 19
can you see? the last ny change to 20.000, and 0:ny is only to 19 not 20. is this a bug? I make something wrong?

回答(3 个)

C
C 2011-9-16
ex=2;
F=[[0 0 0.26 1.3];[0.6 0 0.86 1.3];[1.2 0 1.46 2.6];...
[1.8 0 2.06 2.6];[2.4 0 2.66 2.6]]; % [x1 y1 x2 y2 Mat q]
fanzahl=length(F(:,1));
X=abs(F(1:fanzahl,3)-F(1:fanzahl,1));
Y=abs(F(1:fanzahl,2)-F(1:fanzahl,4));
ey=abs(ex*Y./X);
for i=1:fanzahl
ey(i)
0:ey(i)
end
when I write it more simple, the answer is the same.
the cyclist
the cyclist 2011-9-16
If you type
>> format long
before you run your function, you will see that in the last case, ny is slightly less than 20, which is why you get the output you see.

3 个评论

C
C 2011-9-16
thanks.
but ey(5) should be 20.000000 not 19.99999982
This is not a bug, but it is an issue with exact representation of floating-point numbers in computers. Here is some detailed documentation from Mathworks:
http://www.mathworks.com/support/tech-notes/1100/1108.html.
You could round "ny" to the nearest integer to get what you want.
Here is another place to read more about this issue:
http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

请先登录,再进行评论。

C
C 2011-9-16
yes, I know, but ey(5) should be 20.000000 not 19.99999982
X =
0.260000000000000
0.260000000000000
0.260000000000000
0.260000000000000
0.260000000000000
Y =
1.300000000000000
1.300000000000000
2.600000000000000
2.600000000000000
2.600000000000000
ey =
10.000000000000000
10.000000000000000
20.000000000000000
20.000000000000000
19.999999999999982

类别

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

提问:

C
C
2011-9-16

Community Treasure Hunt

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

Start Hunting!

Translated by