5

Python 3 seems to have some arbitrary limit to Decimal sizes that Python 2 does not. The following code works on Python 2:

Decimal('1e+100000000000000000000') 

But on Python 3 I get:

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

Increasing the precision does not help. Why is this happening? Is there something I can do about it?

12
  • It seems to be related to the precision of your OS. In python2 you get Decimal('infinity') but python3 doesn't seem to allow 'infinity' in decimal. Commented Jun 8, 2016 at 12:57
  • Sorry that I can't help but I have no error with python2 or python3. Could even write a much larger number. Commented Jun 8, 2016 at 12:58
  • @gdlmx what are you basing that on? If it's related to the precision of my OS, why does it not happen on Python 2? Also, Decimal('Infinity') does work. Commented Jun 8, 2016 at 13:03
  • 1
    Actually a decimal.Overflow could be expected rather than a decimal.InvalidOperation, what is always related to an operation. In your example, there is no operation at all. Commented Jun 8, 2016 at 13:07
  • 2
    I am curious to know what 1e+100000000000000000000 is useful for :) Commented Jun 8, 2016 at 13:09

1 Answer 1

4

It would appear that Decimals actually can't hold arbitrarily long numbers:

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

Indeed, I never heard that arbitrarily long numbers was the point of Decimal - just proper rounding and decimals of arbitrary precision. If you want an arbitrarily long number, that's what longs are for, and in Python3 that's just what you've got.

>>> d = 10 ** 100000000000000000000 

(Though it takes a long long while to compute this. My Mac book with I believe a core i5 still hasn't finished after a couple of minutes. Heck, even the string 1, followed by all those zeroes, is going to be really really big.)

For further kicks and grins, I discovered that you can configure the overflow value, apparently, though you still can't get such a whopping big number:

>>> 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 
Sign up to request clarification or add additional context in comments.

5 Comments

It looks like Python 2 allows me to create such large numbers but refuses to operate on them due to the exponent being above Emax.
In python 2, it is possible to specify an arbitrarily large Emax. The same behavior is True in Python 3 if you set sys.modules['_decimal'] = None before importing the decimal module.
@ppperry very interesting. And also sounds like a terrible thing to do ;)
Wanted to add that all programming languages are required to have a max number of significant figures, denoted emax, and a min, denoted emin. This is part of the definition of floating point arithmetic and is not python specific. here is the link to the ieee standard for floating point arithmatic, it is in section 3.3 (EDIT as I wanted to shift-enter but didn't press shift)
Note that should be "all programming languages that implement the IEEE standard for floating point arithmetic" - since not all programming languages have to have float. Or correctly implement the spec. But for all modern, common programming languages that would make sense

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.