-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-72902: speedup Fraction.from_decimal/float in typical cases #133251
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
serhiy-storchaka
merged 6 commits into
python:main
from
skirpichev:opt-Fraction.from_Decimal/72902
May 25, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
caa608f
gh-72902: speedup Fraction.from_Decimal/float in typical cases
skirpichev 1ea8f6a
+1
skirpichev 23d0b52
Merge branch 'main' into opt-Fraction.from_Decimal/72902
skirpichev bf0842d
Apply suggestion from @eendebakpt
skirpichev ba889d8
+ cleanup news
skirpichev cbb2d3f
address review: rewrite without nested if
skirpichev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -339,24 +339,24 @@ def from_float(cls, f): | |||||||||
| Beware that Fraction.from_float(0.3) != Fraction(3, 10). | ||||||||||
|
|
||||||||||
| """ | ||||||||||
| if isinstance(f, float): | ||||||||||
| return cls._from_coprime_ints(*f.as_integer_ratio()) | ||||||||||
| if isinstance(f, numbers.Integral): | ||||||||||
| return cls(f) | ||||||||||
| elif not isinstance(f, float): | ||||||||||
| raise TypeError("%s.from_float() only takes floats, not %r (%s)" % | ||||||||||
| (cls.__name__, f, type(f).__name__)) | ||||||||||
| return cls._from_coprime_ints(*f.as_integer_ratio()) | ||||||||||
| raise TypeError("%s.from_float() only takes floats, not %r (%s)" % | ||||||||||
| (cls.__name__, f, type(f).__name__)) | ||||||||||
|
|
||||||||||
| @classmethod | ||||||||||
| def from_decimal(cls, dec): | ||||||||||
| """Converts a finite Decimal instance to a rational number, exactly.""" | ||||||||||
| from decimal import Decimal | ||||||||||
| if isinstance(dec, Decimal): | ||||||||||
| return cls._from_coprime_ints(*dec.as_integer_ratio()) | ||||||||||
|
Comment on lines
+353
to
+354
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||||||||||
| if isinstance(dec, numbers.Integral): | ||||||||||
| dec = Decimal(int(dec)) | ||||||||||
| elif not isinstance(dec, Decimal): | ||||||||||
| raise TypeError( | ||||||||||
| "%s.from_decimal() only takes Decimals, not %r (%s)" % | ||||||||||
| (cls.__name__, dec, type(dec).__name__)) | ||||||||||
| return cls._from_coprime_ints(*dec.as_integer_ratio()) | ||||||||||
| dec = int(dec) | ||||||||||
| return cls._from_coprime_ints(*dec.as_integer_ratio()) | ||||||||||
| raise TypeError("%s.from_decimal() only takes Decimals, not %r (%s)" % | ||||||||||
| (cls.__name__, dec, type(dec).__name__)) | ||||||||||
|
|
||||||||||
| @classmethod | ||||||||||
| def _from_coprime_ints(cls, numerator, denominator, /): | ||||||||||
|
|
||||||||||
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Library/2025-05-01-16-44-16.gh-issue-72902.19qwJW.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Optimize (~x1.4 speedup) :meth:`fractions.Fraction.from_decimal` and | ||
| :meth:`fractions.Fraction.from_float` for :class:`~decimal.Decimal` and | ||
| :class:`float` inputs, respectively. Patch by Sergey B Kirpichev. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For a subclass of
float,f.as_integer_ratio()doesn't necessarily return a normalised integer pair. I think we should restrict this optimisation to exact floats.The intention is to optimise the common case, and that is certainly (and by a margin) exact
float, not arbitrary subclasses.We then need to keep the fallback code that handles subclasses, as it was before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This affects only float subclasses. The float.as_integer_ration was already documented to return coprime ints.