8000 bpo-43420: Simple optimizations for Fraction's arithmetics by skirpichev · Pull Request #24779 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-43420: Simple optimizations for Fraction's arithmetics #24779

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 13 commits into from
Mar 22, 2021
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
Next Next commit
Merge Fraction._add/sub() to a common helper _add_sub_()
  • Loading branch information
skirpichev committed Mar 14, 2021
commit 598b3c96307618abc71bfe984b1460aca3f51ecc
16 changes: 7 additions & 9 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""Fraction, infinite-precision, real numbers."""

from decimal import Decimal
import functools
import math
import numbers
import operator
Expand Down Expand Up @@ -380,20 +381,17 @@ def reverse(b, a):

return forward, reverse

def _add(a, b):
"""a + b"""
def _add_sub_(a, b, pm=int.__add__):
da, db = a.denominator, b.denominator
return Fraction(a.numerator * db + b.numerator * da,
return Fraction(pm(a.numerator * db, b.numerator * da),
da * db)

_add = functools.partial(_add_sub_)
_add.__doc__ = 'a + b'
__add__, __radd__ = _operator_fallbacks(_add, operator.add)

def _sub(a, b):
"""a - b"""
da, db = a.denominator, b.denominator
return Fraction(a.numerator * db - b.numerator * da,
da * db)

_sub = functools.partial(_add_sub_, pm=int.__sub__)
_sub.__doc__ = 'a - b'
__sub__, __rsub__ = _operator_fallbacks(_sub, operator.sub)

def _mul(a, b):
Expand Down
0