考虑到.5英寸的平均值,我需要所有的浮点(arg)规格吗?在
# Heron of Alexandria method for approximating
# the square root of a number
def heron(num, guess, tolerance):
print "Your guess squared:",guess**2
if guess**2 != num:
print "When squared, the guess:", guess, "is",abs(float(num) - float(guess)**2), "away from", num, "\n"
if abs(float(num) - float(guess)**2) > float(tolerance):
avg_guess = 0.5 * (float(guess) + (float(num) / float(guess)))
return heron(num, avg_guess, tolerance)
print "Given your tolerance, this is Heron's best guess:", guess, "\n"
else:
print guess, "is correct!\n"
我注意到在一定的容差下,这个函数将返回5.0作为答案。。。不知道为什么?在
^{pr2}$从“else:”条件返回5.0,但是
heron(25, 3, .0000001)
从初始嵌套“if:”条件中的公差终止,打印: 考虑到您的容忍度,这是Heron的最佳猜测:5.00000000233。在
目前没有回答
相关问题 更多 >
编程相关推荐