Part 2
Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a).
a = 1 | my_sqrt(a) = 1 | math.sqrt(a) = 1.0 | diff = 0.0
a = 2 | my_sqrt(a) = 1.41421356237 | math.sqrt(a) = 1.41421356237 | diff = 2.22044604925e-16
a = 3 | my_sqrt(a) = 1.73205080757 | math.sqrt(a) = 1.73205080757 | diff = 0.0
a = 4 | my_sqrt(a) = 2.0 | math.sqrt(a) = 2.0 | diff = 0.0
a = 5 | my_sqrt(a) = 2.2360679775 | math.sqrt(a) = 2.2360679775 | diff = 0.0
Modify your program so that it outputs lines for a values from 1 to 25 instead of just 1 to 9.
You should submit a script file and a plain text output file (.txt) that contains the test output. Multiple file uploads are permitted.
import math
def my_sqrt(n):
s=n**0.5
return s
def test_sqrt():
a=1
while a<=25:
print("a=",a,"|","my_sqrt(a)=",my_sqrt(a),"math.sqrt(a)=",math.sqrt(a),"|","diff=",abs(my_sqrt(a)-math.sqrt(a)))
a=a+1
if __name__ == '__main__':
test_sqrt()
Comments
Leave a comment