8000 BUG,TST: Fixed division by 0 status setting · numpy/numpy@ca4ba20 · GitHub
[go: up one dir, main page]

Skip to content

Commit ca4ba20

Browse files
committed
BUG,TST: Fixed division by 0 status setting
1 parent a5e1235 commit ca4ba20

File tree

2 files changed

+12
-29
lines changed

2 files changed

+12
-29
lines changed

numpy/core/src/umath/loops.c.src

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -860,13 +860,8 @@ NPY_NO_EXPORT void
860860
{
861861
BINARY_DEFS
862862

863-
/* When the divisor is a constant, use libdivde for faster division */
863+
/* When the divisor is a constant, use libdivide for faster division */
864864
if (steps[1] == 0) {
865-
/* If divisor is 0, set warning*/
866-
if (*(@type@ *)ip2 == 0) {
867-
npy_set_floatstatus_divbyzero();
868-
}
869-
870865
/* In case of empty array, just return*/
871866
if (n == 0) {
872867
return;
@@ -876,6 +871,7 @@ NPY_NO_EXPORT void
876871

877872
/* If divisor is 0, we need not compute anything*/
878873
if (in2 == 0) {
874+
npy_set_floatstatus_divbyzero();
879875
BINARY_LOOP_SLIDING {
880876
*((@type@ *)op1) = 0;
881877
}
@@ -1422,13 +1418,8 @@ TIMEDELTA_mq_m_divide(char **args, npy_intp const *dimensions, npy_intp const *s
14221418
/* NOTE: This code is similar to array floor divide*/
14231419
BINARY_DEFS
14241420

1425-
/* When the divisor is a constant, use libdivde for faster division */
1421+
/* When the divisor is a constant, use libdivide for faster division */
14261422
if (steps[1] == 0) {
1427-
/* If divisor is 0, set warning*/
1428-
if (*(npy_int64 *)ip2 == 0) {
1429-
npy_set_floatstatus_divbyzero();
1430-
}
1431-
14321423
/* In case of empty array, just return*/
14331424
if (n == 0) {
14341425
return;
@@ -1438,6 +1429,7 @@ TIMEDELTA_mq_m_divide(char **args, npy_intp const *dimensions, npy_intp const *s
14381429

14391430
/* If divisor is 0, we need not compute anything */
14401431
if (in2 == 0) {
1432+
npy_set_floatstatus_divbyzero();
14411433
BINARY_LOOP_SLIDING {
14421434
*((npy_timedelta *)op1) = NPY_DATETIME_NAT;
14431435
}
@@ -1539,16 +1531,8 @@ TIMEDELTA_mm_q_floor_divide(char **args, npy_intp const *dimensions, npy_intp co
15391531
/* NOTE: This code is similar to array floor divide*/
15401532
BINARY_DEFS
15411533

1542-
/* When the divisor is a constant, use libdivde for faster division */
1534+
/* When the divisor is a constant, use libdivide for faster division */
15431535
if (steps[1] == 0) {
1544-
/* If divisor is 0 or NAT, set warning*/
1545-
if (*(npy_timedelta *)ip2 == 0) {
1546-
npy_set_floatstatus_divbyzero();
1547-
}
1548-
else if(*(npy_timedelta *)ip2 == NPY_DATETIME_NAT) {
1549-
npy_set_floatstatus_invalid();
1550-
}
1551-
15521536
/* In case of empty array, just return*/
15531537
if (n == 0) {
15541538
return;
@@ -1558,11 +1542,13 @@ TIMEDELTA_mm_q_floor_divide(char **args, npy_intp const *dimensions, npy_intp co
15581542

15591543
/* If divisor is 0 or NAT, we need not compute anything */
15601544
if (in2 == 0) {
1545+
npy_set_floatstatus_divbyzero();
15611546
BINARY_LOOP_SLIDING {
15621547
*((npy_int64 *)op1) = 0;
15631548
}
15641549
}
15651550
else if (in2 == NPY_DATETIME_NAT) {
1551+
npy_set_floatstatus_invalid();
15661552
BINARY_LOOP_SLIDING {
15671553
*((npy_int64 *)op1) = 0;
15681554
}

numpy/core/tests/test_umath.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ def test_division_int_boundary(self, input_dtype):
275275
a // 0
276276
with pytest.raises( 8000 FloatingPointError):
277277
a //= 0
278-
with pytest.raises(FloatingPointError):
279-
np.array([], dtype=input_dtype) // 0
278+
279+
np.array([], dtype=input_dtype) // 0
280280

281281
@pytest.mark.parametrize(
282282
"dividend,divisor,quotient",
@@ -285,21 +285,18 @@ def test_division_int_boundary(self, input_dtype):
285285
(np.timedelta64(-2,'Y'), np.timedelta64(2,'M'), -12),
286286
(np.timedelta64(-2,'Y'), np.timedelta64(-2,'M'), 12),
287287
(np.timedelta64(2,'M'), np.timedelta64(-2,'Y'), -1),
288-
(np.timedelta64(2,'Y'), np.timedelta64(0,'M'), None),
289-
(np.array([], dtype='timedelta64[Y]'), np.timedelta64('Nat','M'), None),
288+
(np.timedelta64(2,'Y'), np.timedelta64(0,'M'), 0),
290289
(np.timedelta64(2,'Y'), 2, np.timedelta64(1,'Y')),
291290
(np.timedelta64(2,'Y'), -2, np.timedelta64(-1,'Y')),
292291
(np.timedelta64(-2,'Y'), 2, np.timedelta64(-1,'Y')),
293292
(np.timedelta64(-2,'Y'), -2, np.timedelta64(1,'Y')),
294293
(np.timedelta64(-2,'Y'), -2, np.timedelta64(1,'Y')),
295294
6D47 (np.timedelta64(-2,'Y'), -3, np.timedelta64(0,'Y')),
296295
(np.timedelta64(-2,'Y'), 0, np.timedelta64('Nat','Y')),
297-
(np.array([], dtype='timedelta64[Y]'), 0, None),
298296
])
299297
def test_division_int_timedelta(self, dividend, divisor, quotient):
300-
# If either divisor is 0 or quotient is None or Nat, check for division by 0
301-
if divisor and (isinstance(quotient, int) or
302-
not (quotient is None or np.isnat(quotient))):
298+
# If either divisor is 0 or quotient is Nat, check for division by 0
299+
if divisor and (isinstance(quotient, int) or not np.isnat(quotient)):
303300
msg = "Timedelta floor division check"
304301
assert dividend // divisor == quotient, msg
305302

0 commit comments

Comments
 (0)
0