Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
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
9 changes: 7 additions & 2 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6076,8 +6076,13 @@ def _convert_for_comparison(self, other, equality_op=False):
\Z
""", re.VERBOSE | re.IGNORECASE).match

_all_zeros = re.compile('0*$').match
_exact_half = re.compile('50*$').match
# Checks for regex 0*$
def _all_zeros(d_int,prec=0):
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need the prec argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes we need it, since when it gets called in:

def _round_down(self, prec):
    """Also known as round-towards-0, truncate."""
    if _all_zeros(self._int, prec):
        return 0
    else:
        return -1

and

def _round_half_up(self, prec):
    """Rounds 5 up (away from 0)"""
    if self._int[prec] in '56789':
        return 1
    elif _all_zeros(self._int, prec):
        return 0
    else:
        return -1

they both need the argument

return '0' in d_int and d_int.endswith((len(d_int) - prec) * '0')

# Checks for regex 50*$
def _exact_half(d_int, prec=0):
return len(d_int) >= prec + 1 and d_int[prec] == '5' and d_int.endswith(max(len(d_int) - prec - 1, 0) * '0')

##### PEP3101 support functions ##############################################
# The functions in this section have little to do with the Decimal
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Improved performance of :func:`_pydecimal._all_zeros` by an average of
~1.7x and :func:`_pydecimal._exact_half` by ~1.38x. Patch by Marius Juston
Loading