Firstly, the most likely reason your code is running slower than expected is because a becomes a denormalized number. And denormalized numbers are a special case that can work much, much slower. It is also possible that adding 10 ^ 251 and subtracting it again, you change a to 0 and divide zero by something faster (since the result does not need to be calculated).
But real speed is not stupid, adding tiny, tiny numbers that have no effect. When x = several hundred, a will be so small that subtracting a / i from b will not make any difference. Therefore, instead of b - = a / i; you write
double old_b = b; b -= a / i; if (b == old_b) break;
and your time will change from seconds to much less than a millisecond.
source share