Decimal类可以处理的最大数字是多少? [英] What is the largest number the Decimal class can handle?
问题描述
我的程序计算的数学常数 e 不合理。为此,我需要获取非常大的阶乘。
My program calculates the mathematical constant e, which is irrational. In order to do this, I needed to get factorials of very large numbers.
int
无法处理大于170 !. (我发现Google可以处理的最大计算器是170.654259,但是我不确定如何将非整数分解。) float
也不能处理非常大的数字。
int
cannot handle numbers larger than 170!. (I found that the largest Google's calculator can handle is 170.654259, but I'm not sure how a non integer can be factorized.) float
can not handle very large numbers either.
我计算出 e 为750000位数字,而 math.factorial(750000)
为令人难以置信的大量但是,十进制
显然很容易处理。
I calculated e to 750000 digits, and math.factorial(750000)
is a mind-boggling, large number. Yet, Decimal
handled it with apparent ease.
十进制可以有多少个数字
引发 OverflowError
之前的句柄? Python 2和Python 3的大小不同吗?
How large of a number can Decimal
handle before an OverflowError
is raised? Is the size different in Python 2 versus Python 3?
推荐答案
十进制最大的数字是多少?类可以处理吗?
What is the largest number the Decimal class can handle?
最大幅度是无穷大:
>>> from decimal import Decimal
>>> Decimal('Inf')
Decimal('Infinity')
可表示的最大有限数在给定平台上取决于 decimal.MAX_EMAX
:
The largest representable finite number on a given platform depends on decimal.MAX_EMAX
:
>>> from decimal import Context, MAX_EMAX
>>> d = Context(Emax=MAX_EMAX, prec=1).create_decimal('9e'+str(MAX_EMAX))
>>> d.is_finite()
True
>>> d.next_plus()
Decimal('Infinity')
>>> d
Decimal('9E+999999999999999999')
有效位数取决于< a href = https://docs.python.org/3/library/decimal.html#decimal.MAX_PREC> decimal.MAX_PREC
例如用给定的精度计算 e
:
>>> from decimal import Context
>>> Context(prec=60).exp(1)
Decimal('2.71828182845904523536028747135266249775724709369995957496697')
常量( MAX_EMAX
, MAX_PREC
)仅与C实现有关。纯Python版本可以使用更大的值:
The constants (MAX_EMAX
, MAX_PREC
) are only relevant for the C implementation. Pure Python version can use larger values:
>>> from decimal import Context, MAX_EMAX
>>> Context(Emax=MAX_EMAX+1, prec=1).create_decimal('9e'+str(MAX_EMAX+1))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: valid range for Emax is [0, MAX_EMAX]
>>> from _pydecimal import Context, MAX_EMAX
>>> Context(Emax=MAX_EMAX+1, prec=1).create_decimal('9e'+str(MAX_EMAX+1))
Decimal('9E+1000000000000000000')
这篇关于Decimal类可以处理的最大数字是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!