Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.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]_Python_Python 3.x_Decimal - Fatal编程技术网

所有小数位[Python]

所有小数位[Python],python,python-3.x,decimal,Python,Python 3.x,Decimal,我正在尝试创建一个E(数学常数)近似脚本。但它只给我15位小数。然后我添加了一个Decimal()。有没有办法打印所有的小数。(如果没有,限制是多少?) 这是我的密码: from decimal import * e=1 x = input("Iterations:") x=int(x) while 1==1: e=1 + e/x x -= 1 if (x <= 0): break print(Decimal(e)) # only prints

我正在尝试创建一个E(数学常数)近似脚本。但它只给我15位小数。然后我添加了一个
Decimal()。有没有办法打印所有的小数。(如果没有,限制是多少?)


这是我的密码:

from decimal import *
e=1
x = input("Iterations:")
x=int(x)
while 1==1:
    e=1 + e/x
    x -= 1
    if (x <= 0):
        break

print(Decimal(e)) # only prints 50 decimal places
从十进制导入*
e=1
x=输入(“迭代:”)
x=int(x)
而1==1:
e=1+e/x
x-=1

如果(xTry
float64
from。它们提供更高的精度将浮点结果转换为
Decimal
当然是不够的。您必须使用
Decimal
对象执行所有计算,如果您需要更高的精度,您必须告诉
Decimal

In [73]: from decimal import Decimal, getcontext                                        
In [74]: getcontext().prec = 70                                                         
In [75]: e = Decimal(1)                                                                 
In [76]: x = Decimal(200000)                                                            
In [77]: while x>0: 
    ...:     e = Decimal(1)+e/x 
    ...:     x = x-Decimal(1)                                                           
In [78]: e                                                                              
Out[78]: Decimal('2.718281828459045235360287471352662497757247093699959574966967627724076')
In [79]: str(e)[:52]                                                                    
Out[79]: '2.71828182845904523536028747135266249775724709369995'

不,请参阅可能的副本。我的计算机加载10000位数字需要一段时间:D谢谢。