Skip to content
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

BUG: fix mean for float 16 non-array inputs #8524

Merged
merged 1 commit into from
Jan 24, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions numpy/core/_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ def _mean(a, axis=None, dtype=None, out=None, keepdims=False):
ret = um.true_divide(
ret, rcount, out=ret, casting='unsafe', subok=False)
if is_float16_result and out is None:
ret = a.dtype.type(ret)
ret = arr.dtype.type(ret)
elif hasattr(ret, 'dtype'):
if is_float16_result:
ret = a.dtype.type(ret / rcount)
ret = arr.dtype.type(ret / rcount)
else:
ret = ret.dtype.type(ret / rcount)
else:
Expand Down
6 changes: 6 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -4380,6 +4380,12 @@ def setUp(self):
self.omat = np.array([Decimal(repr(r)) for r in self.rmat.flat])
self.omat = self.omat.reshape(4, 5)

def test_python_type(self):
for x in (np.float16(1.), 1, 1., 1+0j):
Copy link
Contributor

@f0k f0k Jan 24, 2017

Choose a reason for hiding this comment

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

To have a test that triggers the bug fixed in this PR, you may want to add [np.float16(1.)] to this tuple (i.e., a float16 wrapped in a list). The others don't run into the is_float16_result and not hasattr(a, "dtype") case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

all tests are scalars wrapped into a list, though just scalars might be useful too

Copy link
Contributor

Choose a reason for hiding this comment

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

all tests are scalars wrapped into a list

Ooops, right. Great then! Good to merge from my perspective. Thank you for taking this!

assert_equal(np.mean([x]), 1.)
assert_equal(np.std([x]), 0.)
assert_equal(np.var([x]), 0.)

def test_keepdims(self):
mat = np.eye(3)
for f in self.funcs:
Expand Down