How to convert a sympy decimal number to a Python decimal?

Question:

In sympy I can for example do:

from sympy import EulerGamma
EulerGamma.n(60)
0.577215664901532860606512090082402431042159335939923598805767

I would like to convert that into a Decimal number without losing any precision.

from decimal import Decimal as D
import decimal
decimal.getcontext().prec = 100
D(EulerGamma.n(60))
TypeError: conversion from Float to Decimal is not supported

What is the right way to do this?

Asked By: graffe

||

Answers:

Try doing D(str(EulerGamma.n(60))). This will construct a Decimal from the object’s string representation.

Answered By: dan04
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.