Skip to content
Prev Previous commit
Next Next commit
_pydecimal: use helpers for computing len(str(q)) < a
  • Loading branch information
picnixz committed Oct 26, 2025
commit efbdc0a788567807eaa945f705c67967c460a43b
34 changes: 2 additions & 32 deletions Lib/_pydecimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1441,20 +1441,7 @@ def _divide(self, other, context):
else:
op2.int *= 10**(op2.exp - op1.exp)
q, r = divmod(op1.int, op2.int)
# See notes for _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
str_q = None # to cache str(q) when possible
if q.bit_length() < context.prec * _LOG_10_BASE_2_LO:
# assert q < 10 ** context.prec
is_valid = True
elif q.bit_length() >= 1 + context.prec * _LOG_10_BASE_2_HI:
# assert q > 10 ** context.prec
is_valid = False
else:
# Handles other cases due to floating point precision loss
# when computing _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
# Computation of str(q) may fail!
str_q = str(q) # we need to compute this in case of success
is_valid = len(str_q) <= context.prec
is_valid, str_q = _is_leq_than_pow10a_use_str(q, context.prec)
Comment thread
picnixz marked this conversation as resolved.
Outdated
if is_valid:
if str_q is None:
str_q = str(q)
Expand Down Expand Up @@ -1615,24 +1602,7 @@ def remainder_near(self, other, context=None):
r -= op2.int
q += 1

# See notes for _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
if q.bit_length() < context.prec * _LOG_10_BASE_2_LO:
# assert q < 10 ** context.prec
is_valid = True
elif q.bit_length() >= 1 + context.prec * _LOG_10_BASE_2_HI:
# assert q > 10 ** context.prec
is_valid = False
else:
# Handles other cases due to floating point precision loss
# when computing _LOG_10_BASE_2_LO and _LOG_10_BASE_2_HI.
# Computation of str(q) or 10 ** context.prec may be slow!
try:
str_q = str(q)
except ValueError:
is_valid = q < 10 ** context.prec
else:
is_valid = len(str_q) <= context.prec
if not is_valid:
if not _is_leq_than_pow10a(q, context.prec):
Comment thread
picnixz marked this conversation as resolved.
Outdated
# assert q >= 10 ** context.prec
# assert len(str(q)) > context.prec
return context._raise_error(DivisionImpossible)
Expand Down