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.

likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
  • What is that you're trying to achieve? – Dan Aug 23 '13 at 14:33
  • It's not a bug, it has to do with [floating-point arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). What you *think* to be a `3` is in fact a [`3.00000000000000044408920985`](http://docs.python.org/2/tutorial/floatingpoint.html). – Schorsch Aug 23 '13 at 14:54

1 Answers1

2

It a floating point number representation issue. When the numbers are constructed in this way, the number you expect to be exactly 3 is off by a small amount (4.440892e-16). This is expected behaviour, not a bug, see: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

James
  • 65,548
  • 14
  • 155
  • 193