Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the largest number the Decimal class can handle?

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 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.

I calculated e to 750000 digits, and math.factorial(750000) is a mind-boggling, large number. Yet, Decimal handled it with apparent ease.

How large of a number can Decimal handle before an OverflowError is raised? Is the size different in Python 2 versus Python 3?

like image 503
Dorian Dore Avatar asked Jan 22 '15 04:01

Dorian Dore


People also ask

What is the largest number in decimal?

There are a total of 10 digits in the decimal number system. Hence, the largest digit in the decimal number system is from one of the above. Clearly, out of the ones given here, 9 is the largest digit. Thus, 9 is the largest digit in the decimal number system.

What is the limit of decimal?

The maximum value of 65 for M means that calculations on DECIMAL values are accurate up to 65 digits. This limit of 65 digits of precision also applies to exact-value numeric literals, so the maximum range of such literals differs from before.

What is the largest value in decimal that can be stored in a byte?

The maximum decimal number that can be represented with 1 byte is 255 or 11111111. An 8-bit word greatly restricts the range of numbers that can be accommodated. But this is usually overcome by using larger words.

What is the maximum decimal number possible to represent with 16 bytes?

For an unsigned short, all 16 bits are used to represent the value, so the largest representable number is 216 − 1 = 65,535.


2 Answers

What is the largest number the Decimal class can handle?

The largest magnitude is infinity:

>>> from decimal import Decimal
>>> Decimal('Inf')
Decimal('Infinity')

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')

The number of significant digits depends on decimal.MAX_PREC e.g., to calculate e with the given precision:

>>> from decimal import Context
>>> Context(prec=60).exp(1)
Decimal('2.71828182845904523536028747135266249775724709369995957496697')

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')
like image 106
jfs Avatar answered Sep 26 '22 12:09

jfs


It depends on the context you provide for the Decimal object. From the library documentation:

class decimal.Context(prec=None, rounding=None, traps=None, flags=None, Emin=None, Emax=None, capitals=1)

Emax and Emin control the bounds of your decimal. If the exponent is greater than Emax or less than Emin there will be an overflow signal. You can look at the fields in decimal.defaultContext to see what they are by default, or decimal.getContext() to see what they are at any given time.

Edit: As @davidism pointed out you don't get exact answers from decimal calculations. The default precision of the module is 28. So all integers up to 999999999999999999999999999999 (28 nines) can be represented exactly, and higher numbers might be rounded.

like image 40
genisage Avatar answered Sep 26 '22 12:09

genisage