如何让python在计算中使用15位以上的小数?

问题描述 投票:0回答:1

this问题中,我声称这种五边形的构造将永远收敛。

我使用 Geogebra here 绘制了前 50 次迭代。 然而,放大第 50 次迭代会导致明显的滞后和渲染不精确,阻碍了进一步的探索。此外,在 GeoGebra 中手动编码构造非常耗时(50 次迭代超过 6 小时)。

我需要使用除 geogebra 之外的其他工具来计算五边形,所以我写了这段代码

import math 
# enter your coordinates here
A=[0,0]
B=[0,0]
C=[0,0]
D=[0,0]
E=[0,0]
print("Number of pentagons")
n=int(input() )
print("Enter A_1")
A[0]=float (input() )
A[1]=float (input() )
print("Enter B_1")
B[0]=float (input() )
B[1]=float (input() )
print("Enter C_1")
C[0]=float (input() )
C[1]=float (input() )
print("Enter D_1")
D[0]=float (input() )
D[1]=float (input() )
print("Enter E_1")
E[0]=float (input() )
E[1]=float (input() )
X=[0,0]
Y=[0,0]
Z=[0,0]
W=[0,0]
T=[0,0]


#defining some useful functions
def v(a,b):
    result= [b[0]-a[0], b[1]-a[1]]
    return result 
def crs(a, b):
    result = [-0.5*((a[0]*b[1])-(a[1]*b[0]))]
    return result
def dis(a,b):
    result =[math.sqrt((a[0]-b[0])**2+(a[1]-b[1])**2)]
    return result 




for i in range (2,n+1):
    AB= dis(A,B)[0]
    BC= dis(B,C)[0]
    CD= dis(C,D)[0]
    DE= dis(D,E)[0]
    EA= dis(E,A)[0]



    #Here to calculate A_n
    x=   CD*crs(v(D,C),v(D,E))[0]
    y=  -CD*crs(v(E,D),v(E,B))[0]+  2*DE*crs(v(C,B),v(C,D))[0]
    z=  -CD*crs(v(C,B),v(C,E))[0]+  2*BC*crs(v(D,C),v(D,E))[0]
    t=   CD*crs(v(C,B),v(C,D))[0]
    X[0]=(x *B[0]+y*C[0]+z*D[0]+t*E[0])/(x+y+z+t)
    X[1]=(x *B[1]+y*C[1]+z*D[1]+t*E[1])/(x+y+z+t)



    #Here to calculate B_n
    x=   DE*crs(v(E,D),v(E,A))[0]
    y=  -DE*crs(v(A,E),v(A,C))[0]+  2*EA*crs(v(D,C),v(D,E))[0]
    z=  -DE*crs(v(D,C),v(D,A))[0]+  2*CD*crs(v(E,D),v(E,A))[0]
    t=   DE*crs(v(D,C),v(D,E))[0]
    Y[0]=(x *C[0]+y*D[0]+z*E[0]+t*A[0])/(x+y+z+t)
    Y[1]=(x *C[1]+y*D[1]+z*E[1]+t*A[1])/(x+y+z+t)



    #Here to calculate C_n
    x=   EA*crs(v(A,E),v(A,B))[0]
    y=  -EA*crs(v(B,A),v(B,D))[0]+  2*AB*crs(v(E,D),v(E,A))[0]
    z=  -EA*crs(v(E,D),v(E,B))[0]+  2*DE*crs(v(A,E),v(A,B))[0]
    t=   EA*crs(v(E,D),v(E,A))[0]
    Z[0]=(x *D[0]+y*E[0]+z*A[0]+t*B[0])/(x+y+z+t)
    Z[1]=(x *D[1]+y*E[1]+z*A[1]+t*B[1])/(x+y+z+t)



    #Here to calculate D_n
    x=   AB*crs(v(B,A),v(B,C))[0]
    y=  -AB*crs(v(C,B),v(C,E))[0]+  2*BC*crs(v(A,E),v(A,B))[0]
    z=  -AB*crs(v(A,E),v(A,C))[0]+  2*EA*crs(v(B,A),v(B,C))[0]
    t=   AB*crs(v(A,E),v(A,B))[0]
    W[0]=(x *E[0]+y*A[0]+z*B[0]+t*C[0])/(x+y+z+t)
    W[1]=(x *E[1]+y*A[1]+z*B[1]+t*C[1])/(x+y+z+t)



    #Here to calculate E_n
    x=   BC*crs(v(C,B),v(C,D))[0]
    y=  -BC*crs(v(D,C),v(D,A))[0]+  2*CD*crs(v(B,A),v(B,C))[0]
    z=  -BC*crs(v(B,A),v(B,D))[0]+  2*AB*crs(v(C,B),v(C,D))[0]
    t=   BC*crs(v(B,A),v(B,C))[0]
    T[0]=(x *A[0]+y*B[0]+z*C[0]+t*D[0])/(x+y+z+t)
    T[1]=(x *A[1]+y*B[1]+z*C[1]+t*D[1])/(x+y+z+t)
    A=X
    B=Y
    C=Z
    D=W
    E=T

    
    print(f"A_{i}= {A}")
    print(f"B_{i}= {B}")
    print(f"C_{i}= {C}")
    print(f"D_{i}= {D}")
    print(f"E_{i}= {E}")  

问题是,在 20-30 次迭代之后,python 给了我这个错误:ZeroDivisionError: float 除以零

我认为这与以下事实有关:经过 20-30 次迭代后,前 15 位数字将相同,因此我需要 python 在所有这些计算中使用超过 50 位小数。

我尝试用谷歌搜索“如何让python使用更多的小数”我尝试了一些解决方案,但没有一个有效。正如您可能从我的代码中看出的那样,我在编码方面更加出色

python decimal precision
1个回答
0
投票

内置的 decimal 包为此提供了精确的支持。改编自文档的简单示例:

>>> from decimal import Decimal
>>> import decimal
>>> decimal.getcontext().prec
28
>>> Decimal(1)/7
Decimal('0.1428571428571428571428571429')
>>> decimal.getcontext().prec = 6
>>> Decimal(1)/7
Decimal('0.142857')

You will need to adapt your library to box numbers with Decimal though.
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.