Skip to content

BUG: Integer overflow in remainder for Windows #19260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions numpy/core/src/umath/loops.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ NPY_NO_EXPORT void
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
if (in2 == 0) {
if (in2 == 0 || (in1 == NPY_MIN_@TYPE@ && in2 == -1)) {
npy_set_floatstatus_divbyzero();
Copy link
Member

Choose a reason for hiding this comment

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

Do we have to set the error flag for the second branch?

*((@type@ *)op1) = 0;
}
Expand All @@ -872,7 +872,13 @@ NPY_NO_EXPORT void
BINARY_LOOP_TWO_OUT {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
/* see FIXME note for divide above */
/*
* FIXME: On x86 at least, dividing the smallest representable integer
* by -1 causes a SIFGPE (division overflow). We treat this case here
* (to avoid a SIGFPE crash at python level), but a good solution would
* be to treat integer division problems separately from FPU exceptions
* (i.e. a different approach than npy_set_floatstatus_divbyzero()).
*/
if (in2 == 0 || (in1 == NPY_MIN_@TYPE@ && in2 == -1)) {
npy_set_floatstatus_divbyzero();
*((@type@ *)op1) = 0;
Expand Down
29 changes: 29 additions & 0 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,35 @@ def test_remainder_basic(self):
else:
assert_(b > rem >= 0, msg)

@pytest.mark.parametrize("input_dtype",
np.sctypes['int'])
def test_remainder_integer(self, input_dtype):
iinfo = np.iinfo(input_dtype)

# Create list with min, 25th percentile, 0, 75th percentile, max
lst = [iinfo.min, iinfo.min//2, iinfo.max//2, iinfo.max]
divisors = [iinfo.min, iinfo.min//2, iinfo.max//2, iinfo.max]
a = np.array(lst, dtype=input_dtype)

for divisor in divisors:
rem_a = np.remainder(a, divisor)
rem_lst = [i % divisor for i in lst]

msg = "Integer arrays remainder check"
assert all(rem_a == rem_lst), msg

for l in lst:
msg = "Integer scalar remainder check"
assert input_dtype(l) % divisor == l % divisor, msg

with np.errstate(divide='raise'):
with pytest.raises(FloatingPointError):
np.remainder(a, 0)
with pytest.raises(FloatingPointError):
np.remainder(a, -1)
with pytest.raises(FloatingPointError):
np.remainder(iinfo.min, -1)

def test_float_remainder_exact(self):
# test that float results are exact for small integers. This also
# holds for the same integers scaled by powers of two.
Expand Down