You should be very carefully when you do iterations with float counters. Just as an example I'll show you what happens in your case (it is a Java program but your case should be the same): click here to run it yourself
double h = 0.1;
System.out.println(10*h-1.0);
System.out.println(h+h+h+h+h+h+h+h+h+h-1.0);
It just prints the difference to one when doing a multiplication vs. seprarate additions.
Since the representation of floats is not exact the result looks like this:
0.0
-1.1102230246251565E-16
Thus if you use this as a looping condition in the latter case there will be an additional iteration (one is not yet reached).
Try to use the counter variable i
which is an integer and you won't run into such issues.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…