有没有一种方法可以在Python上创建更多小数点而无需导入库/模块? [英] Is there a way to create more decimal points on Python without importing a library/module?

查看:45
本文介绍了有没有一种方法可以在Python上创建更多小数点而无需导入库/模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经面临一个挑战,要在python中不使用任何导入模块就计算一个小数点后100位小数.计算2的平方根

I have been set a challenge to calculate a number to 100 decimal points in python without using any imported modules e.g. to calculate square root of 2

由于这是一项学术挑战,这意味着我无法导入小数,sympy,evalf,gmpy2和其他建议,这些建议将从此处的回复中起作用如何显示非理性python中的小数点后第100位?

As it is an academic challenge it means that I cannot import decimal, sympy, evalf, gmpy2 and other suggestions that would work from responses here how can i show an irrational number to 100 decimal places in python?

我也尝试使用范围以0.0000000000001等进行计算,但这需要使用Numpy

I have also tried to do calculations using range in steps of 0.0000000000001 etc., but that would require use of Numpy How to use a decimal range() step value?

当我计算2的平方根时,Python仅显示16个小数位. 1.4142135623746899

When I do calculate square root of 2, Python only shows me 16 decimal places. 1.4142135623746899

我了解Python不会在小数点后保留更多位数的原因有以下原因 https://docs.python.org/3.4/tutorial/floatingpoint.html

I understand that there are reasons behind why Python won't do more decimal places https://docs.python.org/3.4/tutorial/floatingpoint.html

我试图创造性地思考并计算...的平方根20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

I have tried to think creatively and calculate the square root of 20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

但是Python仍然没有返回完整的数字,我得到了这样的答案

but again Python does not return a complete number, I get an answer like this

1.414213562373095e + 50

一种显示潜在承诺的解决方案正在使用格式

A solution that shows potential promise is using format

def mySqrt(x):

r = x
precision = 10 ** (-10)

while abs(x - r * r) > precision:
    r = (r + x / r) / 2

return r

find_square_root_of = 2
answer = format(mySqrt(find_square_root_of), ',.100f')
print(answer)

这给了我答案

1.4142135623746898698271934335934929549694061279296875000000000000000000000000000000000000000000000000000000

但是我需要计算其余的零.有什么建议需要在代码中解决?

But I need the rest of the zeros calculated. Any suggestions on what needs fixing in the code?

推荐答案

整数在python中具有固有的任意精度.您已经找到了 2 * 10 ** 100 的平方根的好主意,并给出了用于执行此操作的算法,但是您随后使用浮点数来实现该算法.如果使用整数,则它基本上可以正常工作(请注意,您需要 2 * 10 ** 200 ,以便在平方根后留101位数字).您只需要对接近截止点时发生的情况进行一些微调即可.这是我的建议:

Integers are natively arbitrary precision in python. You already had a good idea of finding the square root of 2*10**100, and gave an algorithm for doing so, but you are then using floating point numbers to implement that algorithm. If you use integers, then it should basically work (note that you would need 2*10**200 so that you are left with 101 digits after the square root). It will just need a little bit of tweaking for what happens as you approach the cutoff. Here is my suggestion:

x = 2 * 10 ** 200

r = x

def test_diffs(x, r):
    d0 = abs(x - r**2)
    dm = abs(x - (r-1)**2)
    dp = abs(x - (r+1)**2)
    minimised = d0 <= dm and d0 <= dp
    below_min = dp < dm
    return minimised, below_min

while True:
    oldr = r
    r = (r + x // r) // 2

    minimised, below_min = test_diffs(x, r)
    if minimised:
        break

    if r == oldr:
        if below_min:
            r += 1
        else:
            r -= 1
        minimised, _ = test_diffs(x, r)
        if minimised:
            break

print(f'{r // 10**100}.{r % 10**100:0100d}')

赠予:

1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727

为进行比较:

>>> import decimal
>>> decimal.getcontext().prec=101
>>> Decimal("2").sqrt()
Decimal('1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727')

这篇关于有没有一种方法可以在Python上创建更多小数点而无需导入库/模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆