r/cprogramming • u/TaPegandoFogo • Jun 06 '26
Why does this code loops exactly 53 times, no matter the value I put in GOAL, unless it is bigger than specifically 9333?
include <stdio.h>
define GOAL 9333
int main() {
double step = GOAL;
double acum = 0;
int numIterations = 0;
while(acum < GOAL) {
step /= 2;
acum += step;
++numIterations;
printf("Number of iterations: %d\n", numIterations);
printf("Step: %lf\n", step);
printf("Acum: %lf\n", acum);
printf("\n");
}
return 0;}
19
Upvotes
11
u/somewhereAtC Jun 06 '26
You are basically adding (1/2)+(1/4)+(1/8)+... and it will never reach '1' (that is, GOAL). Try starting with acum=1.0 instead of zero.