New Question.用迭代法计算平方根
高洛峰
高洛峰 2016-10-25 14:59:51
0
1
773

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

1.png

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,因为没有基础上这个课真的是好难啊。我不是很清楚怎么能把这个式子代入,当然有大神指点一下代入方法让我自己想一下也是可以哟,现在真是没有头绪。
多谢!
抱拳!

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

Antworte allen(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


Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!