Are there differences? if so, what are they? What are the advantages and disadvantages? If there are other implementations of rational numbers in python, feel free to include those in the comparison.
If you are just looking for an implementation of rational numbers for ordinary numeric calculations then it is better to use fractions.Fraction
which is intended purely for that case. SymPy's Rational
class is somewhat different because it is part of a framework of symbolic expressions and that carries some overhead compared to just an implementation of simple numbers. Compared to the alternatives it only really makes sense to use SymPy's Rational
as part of symbolic calculations with SymPy.
Internally SymPy itself will use its QQ
type for more efficient numeric calculations with rational numbers. If gmpy2 is installed then QQ
refers to gmpy2.mpq
which is much more efficient than either Rational or Fraction. If gmpy2 is not installed then QQ
refers to SymPy's internal PythonMPQ
type which is similar to Fraction
.