Python 使用decimal.get_context().prec给出错误
我想计算浮点数1/919的120位小数Python 使用decimal.get_context().prec给出错误,python,decimal,Python,Decimal,我想计算浮点数1/919的120位小数 import decimal get_context().prec = 120 result = decimal.Decimal(1) / decimal.Decimal(919) print(result) 但这会产生错误: module 'decimal' has no attribute 'get_context' 我该怎么办?运行代码时,它只输出: NameError: name 'get_context' is not defined
import decimal
get_context().prec = 120
result = decimal.Decimal(1) / decimal.Decimal(919)
print(result)
但这会产生错误:
module 'decimal' has no attribute 'get_context'
我该怎么办?运行代码时,它只输出:
NameError: name 'get_context' is not defined
您的问题有两种解决方案:
使用全局上下文
你离得够近了,它是getcontext
而不是getu context
>>从十进制导入十进制,getcontext
>>>getcontext().prec=120
>>>十进制(1)/十进制(919)
十进制('0.001088139281828073993471164309031556039173014145810663764961915125136174102285092491838955386289445048962676822633297062')
使用特定上下文
如果您想要单个操作的精度,或在上下文中具有更大的灵活性,可以创建它们:
来自十进制导入上下文的>>
>>>上下文=上下文(prec=120)
>>>上下文划分(1919)
十进制('0.001088139281828073993471164309031556039173014145810663764961915125136174102285092491838955386289445048962676822633297062')