I have been using python to do algebraic calculations, but at many times when writing 1/2 Python writes 0.5, but I wanted you to write 1/2 and only when needed, convert it to a float.

I'm having a hard time imposing a simplification because Python doesn't recognize 32.0 as being 32, in short example situation where I have.


Since the answer could be


  • 1
    $\begingroup$ That's just how most programming languages work. If you want to do symbolic math, you need to use Maple or Mathematica. $\endgroup$ Feb 1 at 4:39

If you want to do symbolic math, there's a python-module called sympy.

In order the achive what you want here, you could do the following:

from sympy.abc import x
from sympy import simplify
f = (32 - 64 * x)**2 / 4096
print(f) # (32 - 64*x)**2/4096
print(simplify(f)) # (2*x - 1)**2/4
  • $\begingroup$ I've been using the python-module sympy, but it doesn't have simplification. $\endgroup$
    – mhpedro
    Feb 1 at 12:33
  • $\begingroup$ @PEDROMORAIS, it does have simplification. I suggest that you check SymPy documentation. If you have similar questions you could use the mailing list or Gitter. $\endgroup$
    – nicoguaro
    Feb 1 at 16:37
  • $\begingroup$ @nicoguaro Checking the SymPy documentation. I found the command nsimplify()which find a simple representation for a number or, if there are free symbols or if rational=True, then replace Floats with their Rational equivalents. If no change is made and rational is not False then Floats will at least be converted to Rationals. $\endgroup$
    – mhpedro
    Feb 2 at 14:43
  • $\begingroup$ @PEDROMORAIS, I suggest that you ask in Gitter. This is not the place to have this discussion. $\endgroup$
    – nicoguaro
    Feb 2 at 17:32
  • $\begingroup$ @PEDROMORAIS, also. I suggest that you accept the answer. $\endgroup$
    – nicoguaro
    Feb 2 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.