假设我有等式 T = sum(A**n) for n 从 1 到 M。
现在假设我知道 M 和 T,但想要 A。我将如何解决 A?
我想在发生错误时进行指数退避,但我不希望退避所花费的总时间大于 T,也不希望最大重试次数超过 M。所以我需要找到 A .
假设我有等式 T = sum(A**n) for n 从 1 到 M。
现在假设我知道 M 和 T,但想要 A。我将如何解决 A?
我想在发生错误时进行指数退避,但我不希望退避所花费的总时间大于 T,也不希望最大重试次数超过 M。所以我需要找到 A .
对于从 1 到 M 的 n,sum(A**n) 的闭式解是 (A^(M+1) - 1) / (A - 1) - 1。要查看这项工作,让 M = 3 和A = 2。然后 2^1 + 2^2 + 2^3 = 14,并且 (2^4 - 1) / (2 - 1) - 1 = 15 / 1 - 1 = 14。
所以,我们有闭式表达式 T = (A ^ (M + 1) - 1) / (A - 1) - 1。这是一个超越方程,没有闭式解。然而,因为 RHS 在 A 中是单调递增的(A 的值越大,表达式的值就越大),所以我们可以做相当于二分搜索的方法来找到任意精度的答案:
L = 0
H = MAX(T, 2)
A = (L + H) / 2
while |(A ^ (M + 1) - 1) / (A - 1) - 1 - T| > precision
if (A ^ (M + 1) - 1) / (A - 1) - 1 > T then
H = A
else then
L = A
end if
A = (L + H) / 2
loop
示例:T = 14,M = 3,ε = 0.25
L = 0
H = MAX(15, 2) = 14
A = L + H / 2 = 7
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 385 > 0.25
H = A = 7
A = (L + H) / 2 = 3.5
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 44.625 > 0.25
H = A = 3.5
A = (L + H) / 2 = 1.75
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 3.828125 > 0.25
L = A = 1.75
A = (L + H) / 2 = 2.625
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 13.603515625 > 0.25
H = A = 2.625
A = (L + H) / 2 = 2.1875
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 3.440185546875 > 0.25
H = A = 2.1875
A = (L + H) / 2 = 1.96875
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 0.524444580078125 > 0.25
L = A = 1.96875
A = (L + H) / 2 = 2.078125
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 1.371326446533203125 > 0.25
H = A = 2.078125
A = (L + H) / 2 = 2.0234375
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 0.402295589447021484375 > 0.25
H = A = 2.0234375
A = (L + H) / 2 = 1.99609375
|(A ^ (M + 1) - 1) / (A - 1) - 1 - T|
= 0.066299498081207275390625 < 0.25
Solution: 1.99609375