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

2024-09-24 22:25:11 发布

您现在位置:Python中文网/ 问答频道 /正文

在大学作业中,我的任务是把一个数字的平方根计算到小数点后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不会使用更多的小数位数,因为它只是一个近似值https://docs.python.org/3.4/tutorial/floatingpoint.html 是否有任何方法可以在不使用任何导入/库等的情况下使结果更准确


Tags: oftheto方法if作业情况数字