debugging 调试迭代平方根逼近器

gijlo24d  于 2023-05-18  发布在  其他
关注(0)|答案(3)|浏览(136)

我现在正在做一个练习,内容如下:“调试以下代码。测试它的数字,如{-4,0,0.5,1,3,9,34}和更大的数字。

def mySqrt(number, guess, step, tol):
    #We need to take out negative numbers...
    if (number<0):
        print('Error - we do not work with complex numbers here...')
        return float("NaN")
        
    #If we set guess to zero, we have to provide a number - we assume this is the initial call
    if (guess==0):
        if (number>1):        #If we have numbers larger than one, we can safely guess half as the sqrt
            guess=0.5*number
        else:
            guess=number*2    #If we have numbers smaller than one, we need to double our guess
        
    tmp = guess*guess         #Now compute the square of our guess
    if ((tmp-number)>tol):    #Check if the (guess^2 - number) is lower than our tolerance level
        return guess
    else:
        if (tmp>number):      #If our guess was too high, then iterate by calling ourselves again with a slightly lower guess
            return mySqrt(number, (1+step)*guess, step, tol)
        else:                 #Else, our guess was too small, we need to increase the guess for our next call
            return mySqrt(number, (1-step)*guess, step, tol)
            

testVal = 9
print('Squareroot of '+str(testVal)+' is ')
print(mySqrt(testVal,0,0.001, 0.001))

我试着一行一行地检查代码,但我没有得到任何东西,主要是因为我不确定代码实际上应该输出什么。任何建议都是有帮助的。

4nkexdtk

4nkexdtk1#

我越来越接近解决方案:D

def mySqrt(number, guess, step, tol):
    #We need to take out negative numbers...
    if (number<0):
        print('Error - we do not work with complex numbers here...')
        return float("NaN")
        
    #If we set guess to zero, we have to provide a number - we assume this is the initial call
    if (guess==0):
        if (number>1):        #If we have numbers larger than one, we can safely guess half as the sqrt
            guess=0.5*number
        else:
            guess=number*2    #If we have numbers smaller than one, we need to double our guess
        
    tmp = guess*guess         #Now compute the square of our guess
    if ((tmp-number)<tol):    #Check if the (guess^2 - number) is lower than our tolerance level
        return guess
    else:
        if (tmp>number):      #If our guess was too high, then iterate by calling ourselves again with a slightly lower guess
            return mySqrt(number, (1-step)*guess, step, tol)
        else:                 #Else, our guess was too small, we need to increase the guess for our next call
            return mySqrt(number, (1+step)*guess, step, tol)

感谢您发送编修。

o4tp2gmn

o4tp2gmn2#

递归方法是不必要的。你只需要一个简单的循环。就像这样:

def mySqrt(a, epsilon=0.0001):
    if a > 0:
        x = a / 2
        while True:
            y = (x + a / x) / 2
            if abs(y - x) <= epsilon:
                return y
            x = y
    return float('nan')
wd2eg0qa

wd2eg0qa3#

首先,让我们明确什么是什么:
1.公差精度如果结果与预期结果之间的差异小于精确度,则意味着我们找到了解决方案。所以,我们需要改变
(tmp-number)>tol(tmp-number)<tol
1.算法:我们试着猜测什么数字是给定的平方根。然后,我们将其提升到第二等级,并与给定的数字进行比较。如果结果不符合要求,我们应该增加步骤猜测。否则-拿走。因此,我们应该改变:

if (tmp>number):
    return mySqrt(number, (1+step)*guess, step, tol)
else:
    return mySqrt(number, (1-step)*guess, step, tol)```

if (tmp>number):
    return mySqrt(number, (1-step)*guess, step, tol)
else:
    return mySqrt(number, (1+step)*guess, step, tol)

相关问题