如何定义一个十进制类在python中持有1000位数? [英] How to define a decimal class holding 1000 digits in python?
问题描述
我需要一个拥有1000个十进制数字的类来计算一个系列中的pi数。花时间不重要。如何定义 __ add __
& ...功能这样做?
为例,我需要的值可以保存这个数字:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113
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
:))
使用 decimal.Decimal
的数字显示如下:
from decimal import Decimal as dc
>>> x=dc(3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113)
>>> x
Decimal('3.141592653589793115997963468544185161590576171875')
但是我需要一个新类保存所有DIGITS我可以使用加法,除法和函数在它像2 + 1和pi数是一个例子,正是我不需要计算pi数字我想计算超大十进制数!
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!
推荐答案
您必须设置1000个十进制数字的上下文:
You have to set a context with 1000 decimal digits:
context = decimal.Context(prec=1000)
decimal.setcontext(context)
$ b b
从现在起,计算将使用1000位精度。
From now on computations will use 1000 digits precision.
示例:
>>> decimal.setcontext(decimal.Context(prec=1000))
>>> pi = decimal.Decimal('3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113')
>>> pi
Decimal('3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113')
>>> pi + 2
Decimal('5.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113')
需要注意的是:
Note that:
- 您必须使用字符串来初始化
Decimal
,因为如果使用float
解释器将必须首先截断它。 (也相信只有最新版本的decimal
接受一个float
参数在旧版本中, code> Decimal.from_float )。 - 计算期间保留小数位数。
- You have to use strings to initialize the
Decimal
because if you use afloat
the interpreter will have to truncate it first. (also I believe only the most recent versions ofdecimal
accept afloat
argument. In older versions you had to useDecimal.from_float
instead). - The decimal digits are preserved during calculations.
您也可以通过 localcontext
contextmanager:
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.
这篇关于如何定义一个十进制类在python中持有1000位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!