QuantumOscillator
QuantumOscillator

Reputation: 158

using decimal.get_context().prec gives error

I want to calculate decimals upto 120 digits of the float value 1/919.

import decimal  
get_context().prec = 120
result = decimal.Decimal(1) / decimal.Decimal(919)
print(result)

But this gives error:

module 'decimal' has no attribute 'get_context'

What should I do?

Upvotes: 0

Views: 218

Answers (1)

Mengard
Mengard

Reputation: 222

Running your code, it simply outputs:

NameError: name 'get_context' is not defined

There are two solutions to your problem:

Using global context

You were close enough, it was getcontext and not get_context

>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 120
>>> Decimal(1) / Decimal(919)
Decimal('0.00108813928182807399347116430903155603917301414581066376496191512513601741022850924918389553862894450489662676822633297062')

Using specific context

If you want a precision for a single operation, or have more flexibility on your context, you can create them:

>>> from decimal import Context
>>> context = Context(prec=120)
>>> context.divide(1, 919)
Decimal('0.00108813928182807399347116430903155603917301414581066376496191512513601741022850924918389553862894450489662676822633297062')

Upvotes: 1

Related Questions