Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不使用任何库或模块的情况下将数字的平方根设置为100位小数_Python_Python 3.x_Square Root - Fatal编程技术网

Python 如何在不使用任何库或模块的情况下将数字的平方根设置为100位小数

Python 如何在不使用任何库或模块的情况下将数字的平方根设置为100位小数,python,python-3.x,square-root,Python,Python 3.x,Square Root,在大学作业中,我的任务是把一个数字的平方根计算到小数点后100位 我已经找到了一种简单的求平方根的方法,但是在默认情况下输出零之前,输出只需52位小数 def sqrt2(): #if x**y == x to the power of y (or y times multiplication of x e.g. 2**3 == 2*2*2) then to calculate the square root change y to 1/2 #e.g if 4 to the po

在大学作业中,我的任务是把一个数字的平方根计算到小数点后100位 我已经找到了一种简单的求平方根的方法,但是在默认情况下输出零之前,输出只需52位小数

def sqrt2():
    #if x**y == x to the power of y (or y times multiplication of x e.g. 2**3 == 2*2*2) then to calculate the square root change y to 1/2
    #e.g if 4 to the power of 2 == 16 then 16 to the power of 1/2 = 4 
    result = 2**(0.5)
    printResult = format(result, ',.100f')
    
    print(printResult)

sqrt2()
该函数的输出为1.4142135623730951454746218587388284504413604736328125000000000000000000000000000000000000000000000000000000000000000000000000000000 因此,正如您所看到的,它默认为数字中间的零。 我理解为什么Python不会做更多的小数位数,因为它只是一个近似值
有什么方法可以在不使用任何导入/库等的情况下使结果更准确?

这是否回答了您的问题?将您的数字乘以
10**200
,然后使用牛顿法得到整数平方根。然后在正确的位置插入一个小数点。但是如何在正确的位置插入一个小数点?@Chris关于这个问题的所有答案都需要导入或库。我不知道为什么在库如此容易获得的情况下,你会努力解决这个问题,但这不是我的问题。@Dylancrean即使
10**200
的平方根是
10**100
,所以你只需将小数位放在最右边的100位之前。