0

I don't know what's going on but when I use i = 0.1:0.1:7 in my code I get the integers 1 and 2, it then skips 3 but gets 4, 5, 6 and 7 no problems.

x=zeros((t_f/h)+1,1);
x(1,1)=0; 

table=zeros(t_f,1); 

for i=0.1:0.1:7;  
      x(round(i/h)+1,1)=i;
      if ~mod(i,1)
          table(i,1)=i;  
      end
end

Then to test I returned these

table
a=[x(1) x(11) x(21) x(41) x(51) x(61) x(71)]
a=[x(1) x(11) x(21) x(31) x(41) x(51) x(61) x(71)]

It's not finding 3 as an integer because i never is 3, it's 3.000... but 1, 2, 4, 5, 6 and 7 are integers.

4

1 回答 1

2

这是一个浮点数表示问题。当以这种方式构造数字时,您期望恰好为 3 的数字会出现少量偏差 (4.440892e-16)。这是预期的行为,而不是错误,请参阅:http ://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

于 2013-08-23T14:53:23.440 回答