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일

0 개 추천

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일

0 개 추천

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
the cyclist
the cyclist 2011년 9월 16일
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.
the cyclist
the cyclist 2011년 9월 16일
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일

0 개 추천

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