Skip to content

Minor optimization for Fractions.limit_denominator #93730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ def limit_denominator(self, max_denominator=1000000):
n, d = d, n-a*d

k = (max_denominator-q0)//q1
bound1 = Fraction(p0+k*p1, q0+k*q1)
bound2 = Fraction(p1, q1)
bound1 = Fraction(p0+k*p1, q0+k*q1, _normalize=False)
bound2 = Fraction(p1, q1, _normalize=False)
if abs(bound2 - self) <= abs(bound1-self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're here, can you fix-up the spacing around the operator on line 252?

return bound2
else:
Expand Down