There are several things to note here. It seems that only the variable k is changed with each loop.
for (int i = -10000; i < 10000; i++) {
formula = s1 / (v1 + k) + s2;
if (formula == 0) {
break;
}
k += 0.00001;
}
Unfortunately, the value k += 0.00001 cannot be stored exactly as a float (or double), so the compiler usually uses the closest value. If you now add up these not exactly matching double numbers, the error in the sum increases with each loop.
There are now several ways to solve the problem.
1. instead of constantly adding again, it is better to multiply once by the slice index, which increases the accuracy considerably.
2. instead of comparing two floats directly with each other, it would be better to check the difference, whereby the difference often does not become 0, but should fall below a specified value.
Using double instead of float for better accuracy would only shift the problem, not solve it.