python - New Question.用迭代法计算平方根

 mobiledu2502870557 发布于 2022-10-27 09:20

昨天被大神教训说需要帮助就把代码用代码格式写出来,sorry啊,第一次用,刚知道怎么操作,这个project是要用迭代法计算平方根,目的是跟内置数学函数计算的平方根作对比。
professor让用下面这个式子代入计算平方根。

import argparse      # Used in main program to get PIN code from command line
from test_harness import testEQ  # Used in CIS 210 for test cases 

## Constants used by this program

def my_sqrt(number, iterations):
    """
    Generate an iterative approximation to the square root of a number
    args:
        number:  positive number to calculate the square root of
        iterations: number of iterations to perform
    returns:
        approximate value of the square root
    """
    #FIXME: Your code replaces the next line
    value = -12345.0
    return value

def run_tests():
    """
    This function runs a set of tests to help you debug your
    program as you develop it.
    """
    print("**** TESTING --- 5 iterations for sqrt of 1, 10, 100, 1000, 10000")
    testEQ("1.0", my_sqrt(1.0, 5), 1.0)
    testEQ("10.0", my_sqrt(10.0, 5), 3.162277665175675)
    testEQ("100.0", my_sqrt(100.0, 5), 10.032578510960604)
    testEQ("1000.0", my_sqrt(1000.0, 5), 41.24542607499115)
    testEQ("10000.0", my_sqrt(10000.0, 5), 323.0844833048122)
    print("*** End of provided test cases.  Add some of your own? ****")

def main():
    """
    Interaction if run from the command line.
    Magic for now; we'll look at what's going on here
    in the next week or two. 
    """
    parser = argparse.ArgumentParser(description="Iterative approximation for pi")
    parser.add_argument("Number", type=float, help="number (a float)")
    parser.add_argument("-i", "--iterations", type=int, help="iterations (an int)")
    args = parser.parse_args()  # gets arguments from command line
    number = args.Number
    iterations = args.iterations
    value = my_sqrt(number, iterations)
    print("After", iterations, "iterations, sqrt(", number, ") = ", value)

if __name__ == "__main__":
    run_tests()  #FIXME: Comment this out when your program is working
    # main()     #FIXME: Uncomment this when your program is working

stater code 就是这样,当然大部分都写好了,只是需要my_sqrt的function,因为没有基础上这个课真的是好难啊。我不是很清楚怎么能把这个式子代入,当然有大神指点一下代入方法让我自己想一下也是可以哟,现在真是没有头绪。
多谢!
抱拳!

1 个回答
  • python3

    def my_sqrt(number, iterations):
        """
        Generate an iterative approximation to the square root of a number
        args:
            number:  positive number to calculate the square root of
            iterations: number of iterations to perform
        returns:
            approximate value of the square root
        """
        #FIXME: Your code replaces the next line
        n, itr = number, iterations
        x = 1  
        for i in range(itr):
            x = (1/2)*(x+n/x)
            
        value = x 
        return value
    2022-10-27 09:20 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有