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.
-
[Show us](https://stackoverflow.com/help/minimal-reproducible-example) how you are using both modules. How are you evaluating their results? Via elapsed time? Accuracy? What numeric ranges for numerator and denominator are of interest? What higher level domain are you applying them to? – J_H Feb 15 '23 at 23:13
-
@J_H I have no specific application, I just wanted a general comparison. – CatCallofCthulhu Feb 16 '23 at 04:51
-
@AlexK This is not for any academic purpose, I just ask out of curiosity. – CatCallofCthulhu Feb 16 '23 at 04:53
1 Answers
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
.

- 12,649
- 1
- 12
- 14