1

I have a problem with this while on Octave:

while(k<=1.0)
    imshow(cv*k+id*(1-k));
    disp(k);
    k=k+0.04;
    pause(0.1);
endwhile

And this is the output of the values of k:

0
0.040000
0.080000
0.12000
0.16000
0.20000
0.24000
0.28000
0.32000
0.36000
0.40000
0.44000
0.48000
0.52000
0.56000
0.60000
0.64000
0.68000
0.72000
0.76000
0.80000
0.84000
0.88000
0.92000
0.96000

Why does k never reach 1 even if I entered <= in the while condition?

Sardar Usama
  • 19,536
  • 9
  • 36
  • 58
  • 1
    seems to be the floating point issue. Check out these posts: [Why is 24.0000 not equal to 24.0000 in MATLAB?](https://stackoverflow.com/q/686439/5698672) and [Is floating point math broken?](https://stackoverflow.com/q/588004/5698672). Also see how to create a [mre] – Sardar Usama Apr 23 '21 at 23:18
  • 2
    Repeatedly adding a value to a variable enhances the rounding errors in foisting:point arithmetic. Never use a float as a loop condition, use integers instead. Or create your values for k first: `for k = linspace(0, 1, 26);`. – Cris Luengo Apr 24 '21 at 00:11
  • Change `disp(k);` to `fprintf('%.17f\n', k);` – beaker Apr 24 '21 at 14:50

0 Answers0