Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Changes from1 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
NextNext commit
Minor optimization for Fractions.limit_denominator
When we construct the upper and lower candidates in limit_denominator,the numerator and denominator are already relatively prime (and thedenominator positive) by construction, so there's no need to go throughthe usual normalisation in the constructor. This saves a couple ofpotentially expensive gcd calls.Suggested by Michael Scott Asato Cuthbert inGH-93477.
  • Loading branch information
@mdickinson
mdickinson committedJun 11, 2022
commitf69321c69ba16f49da45bd78ea4fd0b4b8a9df27
4 changes: 2 additions & 2 deletionsLib/fractions.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

[8]ページ先頭

©2009-2025 Movatter.jp