Search code examples
pythonsympyfractions

Sympy returns zero for fractions with real numbers


I'm new to Sympy, and have realized it is quite nice for calculating and simplifying algebraic expressions.

However, when when I write fractions of real numbers it returns zero (no problem with fractions of symbols like 'x'). What am I doing wrong?

from sympy import *

1./2
Out[2]: 0.5

1/2
Out[3]: 0

Solution

  • it's because python (2.7) needs a float in denominator or numerator to return a float.

    in python 3.x any division returns a float

    You can also 'fix' in python 2.7 that by using :

    from __future__ import division
    

    In fact python follow the integer division rules and return an integer and not a float number