gpt4 book ai didi

python - 固定点功能错误

转载 作者:行者123 更新时间:2023-11-30 23:31:26 24 4
gpt4 key购买 nike

我的功能几乎是正确的,但是某个地方有一个错误。
你能帮我修复它吗?

def fixedPoint(f, epsilon):
"""
f: that's a function which will return a float
epsilon: it's a float(a small one)

returns the best guess when that guess is less than epsilon
away from f(guess) or after 100 trials, whichever comes first.
"""
guess = 1.0
for i in range(100):
if f(guess) - guess < epsilon:
return guess
else:
guess = f(guess)
return guess

最佳答案

尝试:

if abs(f(guess) - guess) < epsilon:
...

关于python - 固定点功能错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19934578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com