Repository navigation
gh-162709: Signal Overflow for an out-of-range integer quotient in _pydecimal - #162720
SudeepGowda55 wants to merge 3 commits into
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
| if not other._isinfinity(): | ||
| quotient = quotient._fix(context) | ||
| return quotient, remainder._fix(context) |
There was a problem hiding this comment.
| if not other._isinfinity(): | |
| quotient = quotient._fix(context) | |
| return quotient, remainder._fix(context) | |
| return quotient._fix(context), remainder._fix(context) |
The helper already has a special case for Infinity, right? It's not altered.
There was a problem hiding this comment.
_fix() special-cases an infinite self, but when the divisor is infinite the quotient is a finite zero, and _fix() would clamp it where _decimal does not:
c = Context(prec=4, Emax=2, Emin=-4, clamp=1, traps=[])
c.divide_int(Decimal("-8.91"), Decimal("Infinity"))
# _decimal: Decimal("-0"), no flags
# _fix() on the quotient: Decimal("-0.0"), ClampedThat case was most of the mismatches when I compared against _decimal with the unconditional _fix() (483 of 20k random contexts, all clamp=1 with an infinite divisor), so I kept the check.
There was a problem hiding this comment.
I suggest you add test cases for this (and for __floordiv__).
There was a problem hiding this comment.
Added in 5c5e621: test_divide_int_infinite_divisor checks divide_int, Context.divmod, // and divmod() on that context and expects -0 with no flags. It passes for both implementations and fails for _pydecimal with the unconditional _fix(). The __floordiv__ overflow case is in test_divide_int_overflow (via operator.floordiv).
__floordiv__returned the quotient from_divide()as is and__divmod__only ran_fix()on the remainder, so a quotient that fits the precision but notEmaxcame back withoutOverflow. Both now run_fix()on the quotient too.remainder()still goes through_divide()directly and is unchanged.The one exception is a finite number divided by an infinity:
_decimalreturns that 0 as is, without clamping it, so_fix()is skipped there.To check this against
_decimal, I randivide_int,divmodandremainderover 120k random small contexts (all roundings, clamp 0/1, Emax 0-6) with_pydecimalon this branch and_decimal: results and flags match in every case, about 1,450 of them overflows. Before the change about 1 in 14 differed.