Skip to content

Commit efbdc0a

Browse files
committed
_pydecimal: use helpers for computing len(str(q)) < a
1 parent 103fe1e commit efbdc0a

File tree

1 file changed

+2
-32
lines changed

1 file changed

+2
-32
lines changed

Lib/_pydecimal.py

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,20 +1441,7 @@ def _divide(self, other, context):
14411441
else:
14421442
op2.int *= 10**(op2.exp - op1.exp)
14431443
q, r = divmod(op1.int, op2.int)
1444-
# See notes for _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
1445-
str_q = None # to cache str(q) when possible
1446-
if q.bit_length() < context.prec * _LOG_10_BASE_2_LO:
1447-
# assert q < 10 ** context.prec
1448-
is_valid = True
1449-
elif q.bit_length() >= 1 + context.prec * _LOG_10_BASE_2_HI:
1450-
# assert q > 10 ** context.prec
1451-
is_valid = False
1452-
else:
1453-
# Handles other cases due to floating point precision loss
1454-
# when computing _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
1455-
# Computation of str(q) may fail!
1456-
str_q = str(q) # we need to compute this in case of success
1457-
is_valid = len(str_q) <= context.prec
1444+
is_valid, str_q = _is_leq_than_pow10a_use_str(q, context.prec)
14581445
if is_valid:
14591446
if str_q is None:
14601447
str_q = str(q)
@@ -1615,24 +1602,7 @@ def remainder_near(self, other, context=None):
16151602
r -= op2.int
16161603
q += 1
16171604

1618-
# See notes for _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
1619-
if q.bit_length() < context.prec * _LOG_10_BASE_2_LO:
1620-
# assert q < 10 ** context.prec
1621-
is_valid = True
1622-
elif q.bit_length() >= 1 + context.prec * _LOG_10_BASE_2_HI:
1623-
# assert q > 10 ** context.prec
1624-
is_valid = False
1625-
else:
1626-
# Handles other cases due to floating point precision loss
1627-
# when computing _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
1628-
# Computation of str(q) or 10 ** context.prec may be slow!
1629-
try:
1630-
str_q = str(q)
1631-
except ValueError:
1632-
is_valid = q < 10 ** context.prec
1633-
else:
1634-
is_valid = len(str_q) <= context.prec
1635-
if not is_valid:
1605+
if not _is_leq_than_pow10a(q, context.prec):
16361606
# assert q >= 10 ** context.prec
16371607
# assert len(str(q)) > context.prec
16381608
return context._raise_error(DivisionImpossible)

0 commit comments

Comments
 (0)