8000 BUG: Potential fix for divmod(1.0, 0.0) to raise divbyzero and return inf by anirudh2290 · Pull Request #16161 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Potential fix for divmod(1.0, 0.0) to raise divbyzero and return inf #16161

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

Merged
merged 6 commits into from
Nov 25, 2020
Merged
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
102 changes: 95 additions & 7 deletions numpy/core/src/npymath/npy_math_internal.h.src
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,8 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
/**end repeat1**/

/**begin repeat1
* #kind = atan2,hypot,pow,fmod,copysign#
* #KIND = ATAN2,HYPOT,POW,FMOD,COPYSIGN#
* #kind = atan2,hypot,pow,copysign#
* #KIND = ATAN2,HYPOT,POW,COPYSIGN#
*/
#ifdef @kind@@c@
#undef @kind@@c@
Expand All @@ -412,6 +412,32 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
#endif
/**end repeat1**/

/**begin repeat1
* #kind = fmod#
* #KIND = FMOD#
*/
#ifdef @kind@@c@
#undef @kind@@c@
#endif
#ifndef HAVE_MODF@C@
NPY_INPLACE @type@
npy_@kind@@c@(@type@ x, @type@ y)
{
int are_inputs_inf = (npy_isinf(x) && npy_isinf(y));
/* force set invalid flag, doesnt raise by default on gcc < 8 */
if (npy_isnan(x) || npy_isnan(y)) {
npy_set_floatstatus_invalid();
}
if (are_inputs_inf || !y) {
if (!npy_isnan(x)) {
npy_set_floatstatus_invalid();
}
}
return (@type@) npy_@kind@((double)x, (double) y);
}
#endif
/**end repeat1**/

#ifdef modf@c@
#undef modf@c@
#endif
Expand Down Expand Up @@ -473,8 +499,8 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x)
/**end repeat1**/

/**begin repeat1
* #kind = atan2,hypot,pow,fmod,copysign#
* #KIND = ATAN2,HYPOT,POW,FMOD,COPYSIGN#
* #kind = atan2,hypot,pow,copysign#
* #KIND = ATAN2,HYPOT,POW,COPYSIGN#
*/
#ifdef HAVE_@KIND@@C@
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
Expand All @@ -484,6 +510,29 @@ NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
#endif
/**end repeat1**/

/**begin repeat1
* #kind = fmod#
* #KIND = FMOD#
*/
#ifdef HAVE_FMOD@C@
NPY_INPLACE @type@
npy_@kind@@c@(@type@ x, @type@ y)
{
int are_inputs_inf = (npy_isinf(x) && npy_isinf(y));
/* force set invalid flag, doesnt raise by default on gcc < 8 */
if (npy_isnan(x) || npy_isnan(y)) {
npy_set_floatstatus_invalid();
}
if (are_inputs_inf || !y) {
if (!npy_isnan(x)) {
npy_set_floatstatus_invalid();
}
}
return @kind@@c@(x, y);
}
#endif
/**end repeat1**/

#ifdef HAVE_MODF@C@
NPY_INPLACE @type@ npy_modf@c@(@type@ x, @type@ *iptr)
{
Expand Down Expand Up @@ -624,6 +673,38 @@ NPY_INPLACE @type@ npy_logaddexp2@c@(@type@ x, @type@ y)
}
}

/*
* Wrapper function for remainder edge cases
* Internally calls npy_divmod*
*/
NPY_INPLACE @type@
npy_remainder@c@(@type@ a, @type@ b)
{
@type@ mod;
if (NPY_UNLIKELY(!b)) {
mod = npy_fmod@c@(a, b);
} else {
npy_divmod@c@(a, b, &mod);
}
return mod;
}

NPY_INPLACE @type@
npy_floor_divide@c@(@type@ a, @type@ b) {
@type@ div, mod;
if (NPY_UNLIKELY(!b)) {
div = a / b;
if (!a || npy_isnan(a)) {
npy_set_floatstatus_invalid();
} else {
npy_set_floatstatus_divbyzero();
}
} else {
div = npy_divmod@c@(a, b, &mod);
}
return div;
}

/*
* Python version of divmod.
*
Expand All @@ -634,12 +715,19 @@ npy_divmod@c@(@type@ a, @type@ b, @type@ *modulus)
{
@type@ div, mod, floordiv;

/* force set invalid flag, doesnt raise by default on gcc < 8 */
if (npy_isnan(a) || npy_isnan(b)) {
npy_set_floatstatus_invalid();
}
mod = npy_fmod@c@(a, b);

if (!b) {
if (NPY_UNLIKELY(!b)) {
div = a / b;
if (a && !npy_isnan(a)) {
npy_set_floatstatus_divbyzero();
}
/* If b == 0, return result of fmod. For IEEE is nan */
*modulus = mod;
return mod;
return div;
}

/* a - mod should be very nearly an integer multiple of b */
Expand Down
20 changes: 14 additions & 6 deletions numpy/core/src/umath/loops.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -2004,8 +2004,7 @@ NPY_NO_EXPORT void
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
@type@ mod;
*((@type@ *)op1) = npy_divmod@c@(in1, in2, &mod);
*((@type@ *)op1) = npy_floor_divide@c@(in1, in2);
}
}

Expand All @@ -2015,7 +2014,7 @@ NPY_NO_EXPORT void
BINARY_LOOP {
const @type@ in1 = *(@type@ *)ip1;
const @type@ in2 = *(@type@ *)ip2;
npy_divmod@c@(in1, in2, (@type@ *)op1);
*((@type@ *) op1) = npy_remainder@c@(in1, in2);
}
}

Expand Down Expand Up @@ -2360,8 +2359,13 @@ HALF_floor_divide(char **args, npy_intp const *dimensions, npy_intp const *steps
BINARY_LOOP {
const npy_half in1 = *(npy_half *)ip1;
const npy_half in2 = *(npy_half *)ip2;
npy_half mod;
*((npy_half *)op1) = npy_half_divmod(in1, in2, &mod);

float fh1 = npy_half_to_float(in1);
float fh2 = npy_half_to_float(in2);
float div;

div = npy_floor_dividef(fh1, fh2);
*((npy_half *)op1) = npy_float_to_half(div);
}
}

Expand All @@ -2371,7 +2375,11 @@ HALF_remainder(char **args, npy_intp const *dimensions, npy_intp const *steps, v
BINARY_LOOP {
const npy_half in1 = *(npy_half *)ip1;
const npy_half in2 = *(npy_half *)ip2;
npy_half_divmod(in1, in2, (npy_half *)op1);
float fh1 = npy_half_to_float(in1);
float fh2 = npy_half_to_float(in2);
float mod;
mod = npy_remainderf(fh1, fh2);
*((npy_half *)op1) = npy_float_to_half(mod);
}
}

Expand Down
12 changes: 10 additions & 2 deletions numpy/core/src/umath/scalarmath.c.src
6D4E
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ static void
@name@_ctype_floor_divide(@type@ a, @type@ b, @type@ *out) {
@type@ mod;

*out = npy_divmod@c@(a, b, &mod);
if (!b) {
*out = a / b;
} else {
*out = npy_divmod@c@(a, b, &mod);
}
}


Expand Down Expand Up @@ -318,7 +322,11 @@ static void
half_ctype_floor_divide(npy_half a, npy_half b, npy_half *out) {
npy_half mod;

*out = npy_half_divmod(a, b, &mod);
if (!b) {
*out = a / b;
} else {
*out = npy_half_divmod(a, b, &mod);
}
}


Expand Down
7 changes: 7 additions & 0 deletions numpy/core/tests/test_scalarmath.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ def test_float_modulus_corner_cases(self):
# Check nans, inf
with suppress_warnings() as sup:
sup.filter(RuntimeWarning, "invalid value encountered in remainder")
sup.filter(RuntimeWarning, "divide by zero encountered in remainder")
sup.filter(RuntimeWarning, "divide by zero encountered in floor_divide")
sup.filter(RuntimeWarning, "divide by zero encountered in divmod")
sup.filter(RuntimeWarning, "invalid value encountered in divmod")
for dt in np.typecodes['Float']:
fone = np.array(1.0, dtype=dt)
fzer = np.array(0.0, dtype=dt)
Expand All @@ -290,6 +294,9 @@ def test_float_modulus_corner_cases(self):
assert_(np.isnan(rem), 'dt: %s' % dt)
rem = operator.mod(finf, fone)
assert_(np.isnan(rem), 'dt: %s' % dt)
for op in [floordiv_and_mod, divmod]:
div, mod = op(fone, fzer)
assert_(np.isinf(div)) and assert_(np.isnan(mod))

def test_inplace_floordiv_handling(self):
# issue gh-12927
Expand Down
Loading
0