If integer precision doesn't really matter, you can use float
numbers
>>> 3**3**3 7625597484987 >>> 3.**3.**3. 7625597484987.0
However, for large values, they will quickly reach their limits:
>>> 5.**5.**5. Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: (34, 'Numerical result out of range')
You can get much higher decimal
:
>>> import decimal >>> d = decimal.Decimal >>> d(5)**d(5)**d(5) Decimal('1.911012597945477520356404560E+2184') >>> d(10)**d(10)**d(8) Decimal('1.000000000000000000000000000E+100000000')
By default, even those that cannot represent 10**10**10
:
>>> d(10)**d(10)**d(10) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/decimal.py", line 2386, in __pow__ ans = ans._fix(context) File "/usr/lib/python2.7/decimal.py", line 1676, in _fix ans = context._raise_error(Overflow, 'above Emax', self._sign) File "/usr/lib/python2.7/decimal.py", line 3872, in _raise_error raise error(explanation) decimal.Overflow: above Emax
But these restrictions are not fixed. Using getcontext()
, you can make them as large as you want:
>>> decimal.getcontext().Emax = 1000000000000 >>> d(10)**d(10)**d(10) Decimal('1.000000000000000000000000000E+10000000000')
But remember that these numbers are not 100% accurate to the last digit (your computer probably does not even have enough memory to store each digit), so do not be surprised if this happens:
>>> d(10)**d(10)**d(10) == d(10)**d(10)**d(10) + 1000000 True