I have two values x
and y
that represent two time values in seconds. Normally both values will be exactly the same, but in some cases y
can deviate and be smaller or larger depending on the situation. Both values are decremented each iteration by the same amount until they reach zero. I need to change this iteration so that it decreases both values in such a way that they both reach zero at the same time.
The caveat here is that I can't control the initial decrement of x
. Every iteration x
and y
will be decremented by the same interval, and then I need to calculate some additional value to add or subtract from x
to make sure it reaches zero at the same time as y
. Additionally, I have no way of knowing how many iterations it will take to reach zero.
So for example, say we have these values:
x = 20, y = 13, i = ?
i
is the interval that is decremented each iteration. It is a very small number that can fluctuate in value every iteration depending on various factors. Because of this, it is impossible to predict how many iterations it will take to reach zero. Each iteration this would occur first:
x -= i
y -= i
At this point I need to calculate some new value, d
, that can be positive or negative, and add it to x
to move it slightly closer to the value of y
. I know part of the calculation requires taking the diff of x
and y
, but I'm not sure what to do with it from there.
d = ?
x += d
And then eventually, after repeating this for however many iterations it takes, they would both end up at zero at the same time.