Repository navigation
gh-162709: Signal Overflow for an out-of-range integer quotient in _pydecimal - #162720
Open
SudeepGowda55 wants to merge 2 commits into
Open
SudeepGowda55 wants to merge 2 commits into
SudeepGowda55 wants to merge 2 commits into
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
skirpichev
reviewed
Nov 27, 2026
Comment on lines
+1506
to
+1508
| if not other._isinfinity(): | ||
| quotient = quotient._fix(context) | ||
| return quotient, remainder._fix(context) |
Member
There was a problem hiding this comment.
Suggested change
| 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.
Author
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.
Member
There was a problem hiding this comment.
I suggest you add test cases for this (and for __floordiv__).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
__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.