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 get a decimal instead of a fraction in sympy?

I am making a chemistry project for school, I want to use sympy for solving some density and concentration problems.
solveset function returns the value as a fraction, for example:

mL = Symbol("mL")
density = Symbol("p")
mass = 115*g
volume = 100*ml
print(solveset(Eq(density, mass/volume), density))

The output is {23*g/(20*mL)} (fraction) but I want it in the decimal form ({1.15*g/ml})

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 :

You can use the n() method to evaluate the symbolic results to floats. Note that n is an alias of the evalf method. But first, you probably want to extract the result from the set of solutions:

sol = solveset(Eq(density, mass/volume), density)
sol = list(sol)[0].n()
print(sol)
# 1.15*g/mL
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