is this a bug?
Mostra commenti meno recenti
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?
Risposte (3)
C
il 16 Set 2011
the cyclist
il 16 Set 2011
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 Commenti
C
il 16 Set 2011
the cyclist
il 16 Set 2011
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
il 16 Set 2011
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
il 16 Set 2011
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!