How to create a decimal with a large exponent in Python 3?

Python 3 has some arbitrary decimal size limit that Python 2 does not have. The following code runs on Python 2:

Decimal('1e+100000000000000000000')

But on Python 3, I get:

decimal.InvalidOperation: [<class 'decimal.InvalidOperation'>]

Improving accuracy does not help. Why is this happening? Is there anything I can do about it?

+4
source share
1 answer

It seems like Decimalit can't actually contain arbitrarily long numbers:

>>> d = Decimal('10') ** Decimal('100000000000000000000')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
decimal.Overflow: [<class 'decimal.Overflow'>]

In fact, I have never heard that arbitrarily large numbers are a point Decimal- just the right rounding and decimal places of arbitrary precision. If you need an arbitrarily long number, why do you need long ones, and in Python3 what you have.

>>> d = 10 ** 100000000000000000000

( . Mac, , ​​i5 . , 1, , .)

, , ,, :

>>> from decimal import getcontext
>>> getcontext().Emax = 100000000000000000000
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C ssize_t
+3

All Articles