def approximation_of_golden_ratio(eps):
first_k = 1
while True:
next_k = 1 + 1/first_k
if eps > (next_k - first_k):
break
first_k = next_k
return next_k这个函数应该返回第一个黄金比率近似,这比它的前身的eps小。公式是: golden_ratio_approximation =1+1/前身,得到下一个数字近似。
approximation_of_golden_ratio(0.1)返回1.5而不是1.6。
我不知道我的代码有什么问题。
发布于 2020-11-21 17:40:45
我发现了,什么是不对的。我应该用绝对值来得到距离而不是正常的减法。
if eps > abs(next_k - first_k):
def approximation_of_golden_ratio(eps):
first_k = 1
while True:
next_k = 1 + 1/first_k
if eps > abs(next_k - first_k):
break
first_k = next_k
return next_khttps://stackoverflow.com/questions/64946068
复制相似问题