8000 BUG: Fix warning problems of the mod operator by arubiales · Pull Request #19316 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fix warning problems of the mod operator #19316

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 7 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 22 additions & 72 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,copysign#
* #KIND = ATAN2,HYPOT,POW,COPYSIGN#
* #kind = atan2,hypot,pow,fmod,copysign#
* #KIND = ATAN2,HYPOT,POW,FMOD,COPYSIGN#
*/
#ifdef @kind@@c@
#undef @kind@@c@
Expand All @@ -412,32 +412,6 @@ 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**/

Copy link
Member

Choose a reason for hiding this comment

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

These fmod implementations were added in the old divmod-warnings PR. Removing them undoes that change.

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

/**begin repeat1
* #kind = atan2,hypot,pow,copysign#
* #KIND = ATAN2,HYPOT,POW,COPYSIGN#
* #kind = atan2,hypot,pow,fmod,copysign#
* #KIND = ATAN2,HYPOT,POW,FMOD,COPYSIGN#
*/
#ifdef HAVE_@KIND@@C@
NPY_INPLACE @type@ npy_@kind@@c@(@type@ x, @type@ y)
Expand All @@ -510,29 +484,6 @@ 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 @@ -682,8 +633,14 @@ npy_remainder@c@(@type@ a, @type@ b)
{
@type@ mod;
if (NPY_UNLIKELY(!b)) {
/*
* in2 == 0 (and not NaN): normal fmod will give the correct
* result (always NaN). `divmod` may set additional FPE for the
* division by zero creating an inf.
*/
mod = npy_fmod@c@(a, b);
} else {
}
else {
npy_divmod@c@(a, b, &mod);
}
return mod;
Expand All @@ -693,13 +650,14 @@ NPY_INPLACE @type@
npy_floor_divide@c@(@type@ a, @type@ b) {
@type@ div, mod;
if (NPY_UNLIKELY(!b)) {
/*
* in2 == 0 (and not NaN): normal division will give the correct
* result (Inf or NaN). `divmod` may set additional FPE for the modulo
* evaluating to NaN.
*/
div = a / b;
if (!a || npy_isnan(a)) {
npy_set_floatstatus_invalid();
} else {
npy_set_floatstatus_divbyzero();
}
} else {
}
else {
div = npy_divmod@c@(a, b, &mod);
}
return div;
Expand All @@ -715,27 +673,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 (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 */
/* b == 0 (not NaN): return result of fmod. For IEEE is nan */
*modulus = mod;
return div;
return a / b;
}

/* a - mod should be very nearly an integer multiple of b */
div = (a - mod) / b;

/* adjust fmod result to conform to Python convention of remainder */
if (mod) {
if ((b < 0) != (mod < 0)) {
if (isless(b, 0) != isless(mod, 0)) {
mod += b;
div -= 1.0@c@;
}
Expand All @@ -748,7 +698,7 @@ npy_divmod@c@(@type@ a, @type@ b, @type@ *modulus)
/* snap quotient to nearest integral value */
if (div) {
floordiv = npy_floor@c@(div);
if (div - floordiv > 0.5@c@)
if (isgreater(div - floordiv, 0.5@c@))
floordiv += 1.0@c@;
}
else {
Expand Down
10 changes: 2 additions & 8 deletions numpy/core/src/umath/scalarmath.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,13 @@ static void

static void
@name@_ctype_floor_divide(@type@ a, @type@ b, @type@ *out) {
@type@ mod;

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


static void
@name@_ctype_remainder(@type@ a, @type@ b, @type@ *out) {
npy_divmod@c@(a, b, out);
*out = npy_remainder@c@(a, b);
}


Expand Down
37 changes: 27 additions & 10 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,15 @@ def test_floor_division_errors(self, dtype):
# divide by zero error check
with np.errstate(divide='raise', invalid='ignore'):
assert_raises(FloatingPointError, np.floor_divide, fone, fzer)
with np.errstate(invalid='raise'):
assert_raises(FloatingPointError, np.floor_divide, fnan, fone)
assert_raises(FloatingPointError, np.floor_divide, fone, fnan)
assert_raises(FloatingPointError, np.floor_divide, fnan, fzer)
with np.errstate(divide='ignore', invalid='raise'):
np.floor_divide(fone, fzer)

# The following already contain a NaN and should not warn
with np.errstate(all='raise'):
np.floor_divide(fnan, fone)
np.floor_divide(fone, fnan)
np.floor_divide(fnan, fzer)
np.floor_divide(fzer, fnan)

@pytest.mark.parametrize('dtype', np.typecodes['Float'])
def test_floor_division_corner_cases(self, dtype):
Expand Down Expand Up @@ -558,6 +563,9 @@ def test_float_remainder_roundoff(self):
else:
assert_(b > rem >= 0, msg)

@pytest.mark.xfail(sys.platform.startswith("darwin"),
reason="MacOS seems to not give the correct 'invalid' warning for "
"`fmod`. Hopefully, others always do.")
@pytest.mark.parametrize('dtype', np.typecodes['Float'])
def test_float_divmod_errors(self, dtype):
# Check valid errors raised for divmod and remainder
Expand All @@ -578,20 +586,29 @@ def test_float_divmod_errors(self, dtype):
with np.errstate(divide='ignore', invalid='raise'):
assert_raises(FloatingPointError, np.divmod, finf, fzero)
with np.errstate(divide='raise', invalid='ignore'):
assert_raises(FloatingPointError, np.divmod, finf, fzero)
# inf / 0 does not set any flags, only the modulo creates a NaN
np.divmod(finf, fzero)

@pytest.mark.xfail(sys.platform.startswith("darwin"),
reason="MacOS seems to not give the correct 'invalid' warning for "
"`fmod`. Hopefully, others always do.")
@pytest.mark.parametrize('dtype', np.typecodes['Float'])
@pytest.mark.parametrize('fn', [np.fmod, np.remainder])
def test_float_remainder_errors(self, dtype, fn):
fzero = np.array(0.0, dtype=dtype)
fone = np.array(1.0, dtype=dtype)
finf = np.array(np.inf, dtype=dtype)
fnan = np.array(np.nan, dtype=dtype)
with np.errstate(invalid='raise'):
assert_raises(FloatingPointError, fn, fone, fzero)
assert_raises(FloatingPointError, fn, fnan, fzero)
assert_raises(FloatingPointError, fn, fone, fnan)
assert_raises(FloatingPointError, fn, fnan, fone)

# The following already contain a NaN and should not warn.
with np.errstate(all='raise'):
with pytest.raises(FloatingPointError,
match="invalid value"):
fn(fone, fzero)
fn(fnan, fzero)
fn(fzero, fnan)
fn(fone, fnan)
fn(fnan, fone)

def test_float_remainder_overflow(self):
a = np.finfo(np.float64).tiny
Expand Down
0