Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading