Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.6k views
in Technique[技术] by (71.8m points)

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

2 Answers

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
0 votes
by (100 points)

My Prasonal information is Decimal numbers can be represented exactly. In contrast, numbers like 1.1 and 2.2 do not have exact representations in binary floating point. End users typically would not expect 1.1 + 2.2 to display as 3.3000000000000003 as it does with binary floating point.


by
Let me again remind you, that playing private servers won’t ban your main account. Try out them and enjoy the latest features and sure brawlers, that you wish to use in your main game.
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...