Home > database >  How to convert a sympy decimal number to a Python decimal?
How to convert a sympy decimal number to a Python decimal?

Time:11-29

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?

CodePudding user response:

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

  • Related