Author asmeurer
Recipients Sergey.Kirpichev, asmeurer, mark.dickinson, rhettinger
Date 2021-03-07.22:34:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
I'm surprised to hear that the "typical use-case" of Fraction is fractions converted from floats. Do you have evidence in the wild to support that? 

I would expect any application that uses fractions "generically" to run into the same sorts of problems SymPy does. The issue is that the sum or product of two unrelated fractions has a denominator that is ~ the product of the denominators of each term. So they tend to grow large, unless there is some structure in the terms that results in lots of cancellation. That's why real world numeric typically doesn't use exact arithmetic, but there are legitimate use-cases for it (computer algebra being one).

This actually also applies even if the denominators are powers of 2. That's why arbitrary precision floating point numbers like Decimal or mpmath.mpf limit the precision, or effectively, the power of 2 in the denominator. 

By the way, the "algorithm" here really isn't that complicated. I didn't even realize it had a name. The idea is that for a/b * c/d, if a/b and c/d are already in lowest terms, then the only cancellation that can happen is from a/d or from c/b. So instead of computing gcd(a*c, b*d), we only compute gcd(a, d) and gcd(c, b) and cancel them off the corresponding terms. It turns out to be faster to take two gcds of smaller numbers than one gcd of big ones. The algorithm for addition is a bit more complicated, at least to see that it is correct, but is still not that bad (the paper linked in the OP explains it clearly in one paragraph). It's far less complicated than, for example, Lehmer's gcd algorithm (which is implemented in math.gcd).
Date User Action Args
2021-03-07 22:34:24asmeurersetrecipients: + asmeurer, rhettinger, mark.dickinson, Sergey.Kirpichev
2021-03-07 22:34:24asmeurersetmessageid: <>
2021-03-07 22:34:24asmeurerlinkissue43420 messages
2021-03-07 22:34:24asmeurercreate