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개)
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
2011년 9월 16일
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
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
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!