“Square Roots”循环:
while True:
y = (x+ a/x) / 2
if y == x:
return x
x = y
从“Square Roots”复制循环并将其封装在名为mysqrt的函数中,该函数将 a 作为参数,选择合理的 x 值,并返回估计 a 的平方根。 要测试它,编写一个名为test_square_root的函数,它打印一个如下表:
a mysqrt(a) math.sqrt(a) diff
- --------- ------------ ----
1.0 1.0 1.0 0.0
2.0 1.41421356237 1.41421356237 2.22044604925e-16
3.0 1.73205080757 1.73205080757 0.0
4.0 2.0 2.0 0.0
5.0 2.2360679775 2.2360679775 0.0
6.0 2.44948974278 2.44948974278 0.0
7.0 2.64575131106 2.64575131106 0.0
8.0 2.82842712475 2.82842712475 4.4408920985e-16
9.0 3.0 3.0 0.0
这是我写的:
import math
def mysqrt(a):
x = a/5
while True:
y = (x+ a/x) / 2
if y == x:
return x
x = y
def test_square_root():
a = 1.0
print('a', repr(mysqrt(a)).rjust(6), repr(math.sqrt(a)).rjust(12), 'diff'.rjust(10))
print("- --------- ------------ ----")
while a < 10.0:
print(a, " ", mysqrt(a), " ", math.sqrt(a), " ", abs(mysqrt(a)-math.sqrt(a)))
a += 1
test_square_root()
这是我得到的:
a 1.0 1.0 diff
- --------- ------------ ----
1.0 1.0 1.0 0.0
2.0 1.414213562373095 1.4142135623730951 2.220446049250313e-16
3.0 1.7320508075688772 1.7320508075688772 0.0
4.0 2.0 2.0 0.0
5.0 2.23606797749979 2.23606797749979 0.0
6.0 2.449489742783178 2.449489742783178 0.0
7.0 2.6457513110645907 2.6457513110645907 0.0
8.0 2.82842712474619 2.8284271247461903 4.440892098500626e-16
9.0 3.0 3.0 0.0
如您所见,阵列没有整齐排列。如果元素长度不相同,如何打印数组?谢谢!
答案 0 :(得分:1)
试试这个代码,它会得到最接近你需要的格式:
import math
epsilon= 0.00000000001
def mysqrt(a):
x= a/2
while True:
y = (x + a/x) / 2
if abs(y-x) < epsilon:
return y
break
x = y
def right_digit(n):
'''Takes a floating point number and returns its right
most digit in 'str' format'''
trunc= f'{n:.11f}'
dig= trunc[len(trunc)-1]
return dig
def test_square_root(n):
print('a', ' '*1, 'mysqrt(a)', ' '*3, 'math.sqrt(a)', ' diff')
print('-', ' '*1, '-'*9, ' '*3, '-'*12, ' ' + '-'*4)
for i in range(9):
x= i+1
print(f'{x:0.1f}', end=' ')
if mysqrt(x)- int(mysqrt(x)) < 0.001:
y=1
elif right_digit(mysqrt(x)) == '0':
y=10
else:
y=11
print(f'{mysqrt(x):<13.{y}f}', end=' ')
print(f'{mysqrt(x):<13.{y}f}', end=' ')
diff= math.sqrt(x) - mysqrt(x)
print(f'{diff:.12g}')
test_square_root(9)
结果表如下所示:
a mysqrt(a) math.sqrt(a) diff
- --------- ------------ ----
1.0 1.0 1.0 0
2.0 1.41421356237 1.41421356237 2.22044604925e-16
3.0 1.73205080757 1.73205080757 0
4.0 2.0 2.0 0
5.0 2.2360679775 2.2360679775 0
6.0 2.44948974278 2.44948974278 0
7.0 2.64575131106 2.64575131106 0
8.0 2.82842712475 2.82842712475 4.4408920985e-16
9.0 3.0 3.0 0
我使用 f-strings 来格式化这个表,但你也可以使用 .format() 方法。您必须记住两种特殊情况:
您可以在上面的代码中看到 test_square_root 函数中包含 3 个条件语句来处理这些问题。
答案 1 :(得分:0)
调整print语句,分别使用{0:.8f} {0:.10f}和{0:.4f}或您想要的coloumn宽度将每个元素打印到固定的小数位数。
答案 2 :(得分:0)
您可以在打印语句中使用标签\t
:
print("\t", a, "\t", y, "\t", square_method, "\t" "diff:", diff,)
答案 3 :(得分:0)
这是我的代码,看起来不错:
from decimal import *
import math
def mysquare_root(a):
x = 1
while True:
#print(x)
y = (x + a/x) / 2
if x == y:
break
x = y
return (x)
def square_root(a):
x = math.sqrt(a)
return x
def difference(a):
d = square_root(a) - mysquare_root(a)
return d
# the below function prints the table, but the alignment is not good.
def list_func():
for a in range(1, 10):
print(float(a), " " * 20, f"{mysquare_root(a)}", " "*20, f"{square_root(a)}"," "*20,f"{difference(a)}")
list_func()
# by using the below functions ("dec_1, dec_2") we are fixing the decimals after point to
#..........'15' numbers for uniform alignment.
# the 1st function is for square_root which is built in function
# 2nd is for alignment of difference after built_in_ function values.
def dec_1(a):
d = Decimal(mysquare_root(a))
#print(d)
d1 = round(d, 15)
#print(d1)
#print(d1)
return Decimal(d1)
def dec_2(a):
d = Decimal(square_root(a))
#print(d)
d1 = round(d, 15)
#print(d1)
return Decimal(d1)
def print_it():
for a in range(1, 10):
print(float(a), " " * 20, f"{dec_1(a)}", ' '*9, f"{dec_2(a)}", ' '*9, f"{difference(a)}")
print_it()
这是我得到的解决方案:
1.0 1.000000000000000 1.000000000000000 0.0
2.0 1.414213562373095 1.414213562373095 2.220446049250313e-16
3.0 1.732050807568877 1.732050807568877 0.0
4.0 2.000000000000000 2.000000000000000 0.0
5.0 2.236067977499790 2.236067977499790 0.0
6.0 2.449489742783178 2.449489742783178 0.0
7.0 2.645751311064591 2.645751311064591 0.0
8.0 2.828427124746190 2.828427124746190 4.440892098500626e-16
9.0 3.000000000000000 3.000000000000000 0.0
答案 4 :(得分:-1)
这是我想出的完美解决方案:
from decimal import *
import math
# below function is used to find the square roots of a number using the newton's method(i.e; using formula)
def mysquare_root(a):
x = 1
while True:
y = (x + a/x) / 2
if x == y:
break
x = y
return x
# below function is used to find the square roots of a number using the built_in function
def square_root(a):
x = math.sqrt(a)
return x
# below function gives the difference between thr toe functions above
def difference(a):
d = square_root(a) - mysquare_root(a)
return d
# Normally we can print the values as usual by giving selected no. of spaces between one and other values
# but, because of different float values, it is not possible to print uniformly as a table.
# In below function we use the decimal to over come the possibility.
def dec_1(a):
# this function is to print the values of function 'mysquare_root()' uniformly as a table.
# in order to access the decimals of a value we use 'Decimal' in_built function and we can round it off.
d = Decimal(mysquare_root(a))
#print(d)
# here we are rounding off the decimal values to '15' after point.
d1 = round(d, 15)
#print(d1)
# if we round off all the values, even perfect square will have '15' zeros after point. we don't want it.
# so, we are rounding off the perfect square values to '0' decimal values after point
if d1 - Decimal(mysquare_root(a)) == 0:
d1 = round(d, 0)
#print(d1)
# in order to get one decimal value after point, simply we return a float value
return float(d1)
#print(d1)
# we are return value which is rounded to certain values after point.
return (d1)
#print(dec_1(2))
# the below function works same as above function, but for in_built method to find square root.
def dec_2(a):
# This function is working for to print the values of function 'square_root()'
d = Decimal(square_root(a))
#print(d)
d1 = round(d, 15)
if d1 - Decimal(mysquare_root(a)) == 0:
d1 = round(d, 0)
return float(d1)
#print(d1)
return Decimal(d1)
#print(dec_2(2))
def print_it():
# below two print statements arranged as per requirement.
print("numbers", " "*14, 'mysquare_root', ' '*8, 'square_root', ' '*10, "difference")
print("-------", " " * 14, '-------------', ' ' * 8, '-----------', ' ' * 10, "----------")
for a in range(1, 10):
# here we are going to print square roots 1-9
# here we are giving the 18 spaces between the one float value to another in table.
# if there is only 15 decimal values after point, we already covered 14 position after float value, so we will give 4 spaces
# here to do spacing between values, we just comparing float value and int value to get how much space is needed.
print(float(a), " " * 18, f'{dec_1(a)}', " "*18 if dec_1(a) == int(dec_1(a)) else " "* 4, f'{dec_2(a)}', " "*18 if dec_1(a) == int(dec_2(a)) else " "* 4, f"{difference(a)}")
print_it()
这是我的输出
numbers mysquare_root square_root difference
------- ------------- ----------- ----------
1.0 1.0 1.0 0.0
2.0 1.414213562373095 1.414213562373095 2.220446049250313e-16
3.0 1.732050807568877 1.732050807568877 0.0
4.0 2.0 2.0 0.0
5.0 2.236067977499790 2.236067977499790 0.0
6.0 2.449489742783178 2.449489742783178 0.0
7.0 2.645751311064591 2.645751311064591 0.0
8.0 2.828427124746190 2.828427124746190 4.440892098500626e-16
9.0 3.0 3.0 0.0
我试图为您提供清晰的代码。我希望你能得到它。