Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-101773: Optimize creation of Fractions in private methods#101780
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
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
13 commits Select commitHold shift + click to select a range
9cc62f7 gh-101773: Optimize creation of Fraction's in private methods
skirpichev836d3a6 Merge branch 'main' into opt-fractions-new
skirpichev3625d34 Adjust signature of _from_pair() and add docstring
skirpichev9e66b5a Use new helper in from_float/Decimal
skirpichev4324391 Restore back _normalize kwarg for the Fraction.__new__()
skirpichevb0dca38 Amend 3625d34155
skirpichev3a8506d Nitpick on docstring (int's -> ints)
skirpichev13262af Revert "Restore back _normalize kwarg for the Fraction.__new__()"
skirpichev4e8977a Rename: _from_pair -> _from_coprime_ints
skirpichev57ac373 Merge branch 'main' into opt-fractions-new
skirpichev4b4a386 Update Misc/NEWS.d/next/Library/2023-02-10-11-59-13.gh-issue-101773.J…
skirpichev580b1be +1
skirpichev53d5e94 Tweak docstring wording
mdickinsonFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Rename: _from_pair -> _from_coprime_ints
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit4e8977af55780ef517d8600719caaf6c2e0d23d9
There are no files selected for viewing
44 changes: 22 additions & 22 deletionsLib/fractions.py
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 |
|---|---|---|
| @@ -300,7 +300,7 @@ def from_float(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()) | ||
| @classmethod | ||
| def from_decimal(cls, dec): | ||
| @@ -312,10 +312,10 @@ def from_decimal(cls, dec): | ||
| 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()) | ||
| @classmethod | ||
| def_from_coprime_ints(cls, numerator, denominator, /): | ||
| """Convert a pair of ints to a rational number, for internal use. | ||
| The ratio of integers should be in lowest terms and | ||
| @@ -391,9 +391,9 @@ def limit_denominator(self, max_denominator=1000000): | ||
| # the distance from p1/q1 to self is d/(q1*self._denominator). So we | ||
| # need to compare 2*(q0+k*q1) with self._denominator/d. | ||
| if 2*d*(q0+k*q1) <= self._denominator: | ||
| return Fraction._from_coprime_ints(p1, q1) | ||
| else: | ||
| return Fraction._from_coprime_ints(p0+k*p1, q0+k*q1) | ||
| @property | ||
| def numerator(a): | ||
| @@ -714,13 +714,13 @@ def _add(a, b): | ||
| nb, db = b._numerator, b._denominator | ||
| g = math.gcd(da, db) | ||
| if g == 1: | ||
| return Fraction._from_coprime_ints(na * db + da * nb, da * db) | ||
| s = da // g | ||
| t = na * (db // g) + nb * s | ||
| g2 = math.gcd(t, g) | ||
| if g2 == 1: | ||
| return Fraction._from_coprime_ints(t, s * db) | ||
| return Fraction._from_coprime_ints(t // g2, s * (db // g2)) | ||
| __add__, __radd__ = _operator_fallbacks(_add, operator.add) | ||
| @@ -730,13 +730,13 @@ def _sub(a, b): | ||
| nb, db = b._numerator, b._denominator | ||
| g = math.gcd(da, db) | ||
| if g == 1: | ||
| return Fraction._from_coprime_ints(na * db - da * nb, da * db) | ||
| s = da // g | ||
| t = na * (db // g) - nb * s | ||
| g2 = math.gcd(t, g) | ||
| if g2 == 1: | ||
| return Fraction._from_coprime_ints(t, s * db) | ||
| return Fraction._from_coprime_ints(t // g2, s * (db // g2)) | ||
| __sub__, __rsub__ = _operator_fallbacks(_sub, operator.sub) | ||
| @@ -752,7 +752,7 @@ def _mul(a, b): | ||
| if g2 > 1: | ||
| nb //= g2 | ||
| da //= g2 | ||
| return Fraction._from_coprime_ints(na * nb, db * da) | ||
| __mul__, __rmul__ = _operator_fallbacks(_mul, operator.mul) | ||
| @@ -774,7 +774,7 @@ def _div(a, b): | ||
| n, d = na * db, nb * da | ||
| if d < 0: | ||
| n, d = -n, -d | ||
| return Fraction._from_coprime_ints(n, d) | ||
| __truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv) | ||
| @@ -811,17 +811,17 @@ def __pow__(a, b): | ||
| if b.denominator == 1: | ||
| power = b.numerator | ||
| if power >= 0: | ||
| return Fraction._from_coprime_ints(a._numerator ** power, | ||
| a._denominator ** power) | ||
| elif a._numerator > 0: | ||
| return Fraction._from_coprime_ints(a._denominator ** -power, | ||
| a._numerator ** -power) | ||
| elif a._numerator == 0: | ||
| raise ZeroDivisionError('Fraction(%s, 0)' % | ||
skirpichev marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| a._denominator ** -power) | ||
| else: | ||
| return Fraction._from_coprime_ints((-a._denominator) ** -power, | ||
| (-a._numerator) ** -power) | ||
| else: | ||
| # A fractional power will generally produce an | ||
| # irrational number. | ||
| @@ -845,15 +845,15 @@ def __rpow__(b, a): | ||
| def __pos__(a): | ||
| """+a: Coerces a subclass instance to Fraction""" | ||
| return Fraction._from_coprime_ints(a._numerator, a._denominator) | ||
| def __neg__(a): | ||
| """-a""" | ||
| return Fraction._from_coprime_ints(-a._numerator, a._denominator) | ||
| def __abs__(a): | ||
| """abs(a)""" | ||
| return Fraction._from_coprime_ints(abs(a._numerator), a._denominator) | ||
| def __int__(a, _index=operator.index): | ||
| """int(a)""" | ||
2 changes: 1 addition & 1 deletionLib/test/test_numeric_tower.py
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
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.