Search code examples
pythonclasspython-3.xpython-3.3bigdecimal

How to define a decimal class holding 1000 digits in python?


I need a class holding 1000 decimal digits to calculate something like pi number in a series. Taking time is not important. How can I define __add__ & ... functions to do this? For example I need a value can hold this number: 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113

:))

This number using decimal.Decimal shows like this:

from decimal import Decimal as dc
>>> x=dc(3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113)
>>> x
Decimal('3.141592653589793115997963468544185161590576171875')

But I need a new class holding all DIGITS and I can use adding, dividing and ... functions in it like 2+1 and pi number is an example of that and exactly I don't need to calculate pi number I wanted to calculate extra large decimal numbers!


Solution

  • You have to set a context with 1000 decimal digits:

    context = decimal.Context(prec=1000)
    decimal.setcontext(context)
    

    From now on computations will use 1000 digits precision.

    Example:

    >>> decimal.setcontext(decimal.Context(prec=1000))
    >>> pi = decimal.Decimal('3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113')
    >>> pi
    Decimal('3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113')
    >>> pi + 2
    Decimal('5.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113')
    

    Note that:

    • You have to use strings to initialize the Decimal because if you use a float the interpreter will have to truncate it first. (also I believe only the most recent versions of decimal accept a float argument. In older versions you had to use Decimal.from_float instead).
    • The decimal digits are preserved during calculations.

    You can also use the context locally via the localcontext contextmanager:

    context = decimal.Context(prec=1000)
    
    with decimal.localcontext(context):
        # here decimal uses 1000 digits for computations
        pass
    
    # here the default context is restored.