Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: Fix rounding of denormals in double and float to half casts … #12722

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
Jan 24, 2019
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
TST: half casting lower bits are not lost for denormal results
The first test only tested the off by one, this one specifically tests
that all bits are used to decide if "round to nearest even" should
be used, in the example of rounding towards 0.
  • Loading branch information
seberg committed Jan 12, 2019
commit 300df9dfc9a38bf84a22ba07659912ff34c8dac5
25 changes: 24 additions & 1 deletion 25 numpy/core/tests/test_half.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_half_conversions(self):

@pytest.mark.parametrize("offset", [None, "up", "down"])
@pytest.mark.parametrize("shift", [None, "up", "down"])
@pytest.mark.parametrize("float_t", [np.float32,])
@pytest.mark.parametrize("float_t", [np.float32, np.float64])
def test_half_conversion_denormal_rounding(self, float_t, shift, offset):
# Assumes that round to even is used during casting.
f16s_patterns = np.arange(0, 0x401, dtype=np.uint16)
Expand Down Expand Up @@ -121,6 +121,29 @@ def test_half_conversion_denormal_rounding(self, float_t, shift, offset):

assert_equal(res_patterns, cmp_patterns)

@pytest.mark.parametrize(["float_t", "uint_t", "bits"],
[(np.float32, np.uint32, 23),
(np.float64, np.uint64, 52)])
def test_half_conversion_denormal_round_even(self, float_t, uint_t, bits):
# Test specifically that all bits are considered when deciding
# whether round to even should occur (i.e. no bits are lost at the
# end. Compare also gh-12721. The most bits can get lost for the
# smallest denormal:
smallest_value = np.uint16(1).view(np.float16).astype(float_t)
assert smallest_value == 2**-24

# Will be rounded to zero based on round to even rule:
rounded_to_zero = smallest_value / float_t(2)
assert rounded_to_zero.astype(np.float16) == 0

# The significand will be all 0 for the float_t, test that we do not
# lose the lower ones of these:
for i in range(bits):
# slightly increasing the value should make it round up:
larger_pattern = rounded_to_zero.view(uint_t) | uint_t(1 << i)
larger_value = larger_pattern.view(float_t)
assert larger_value.astype(np.float16) == smallest_value

def test_nans_infs(self):
with np.errstate(all='ignore'):
# Check some of the ufuncs
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.