固定点功能错误

时间:2013-11-12 16:29:19

标签: python

我的功能几乎是正确的,但是某处有一个错误。
你可以帮我解决它吗?

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

1 个答案:

答案 0 :(得分:3)

尝试:

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