1

我的功能几乎是正确的,但是某处存在错误。
你能帮我修一下吗?

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
4

1 回答 1

3

尝试:

if abs(f(guess) - guess) < epsilon:
    ...
于 2013-11-12T16:33:28.380 回答