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

Check for overflow when casting non-int/float types in Numpy arrays #1591

Closed
Zac-HD opened this issue Sep 21, 2018 · 0 comments
Closed

Check for overflow when casting non-int/float types in Numpy arrays #1591

Zac-HD opened this issue Sep 21, 2018 · 0 comments
Assignees
Labels
legibility make errors helpful and Hypothesis grokable

Comments

@Zac-HD
Copy link
Member

Zac-HD commented Sep 21, 2018

In this StackOverflow question, the lack of overflow warnings for complex numbers led to some confusion.

We should add a check for overflow in arrays of dtype complex64 (complex128 and larger can't overflow, because we only generate 64-bit floats). Here's the code that currently handles this - it's probably sufficient to apply the floats check to both real and imaginary parts:

# Used by self.insert_element to check that the value can be stored
# in the array without e.g. overflowing. See issue #1385.
if dtype.kind in (u'i', u'u'):
self.check_cast = lambda x: np.can_cast(x, self.dtype, 'safe')
elif dtype.kind == u'f' and dtype.itemsize == 2:
max_f2 = (2. - 2 ** -10) * 2 ** 15
self.check_cast = lambda x: \
(not np.isfinite(x)) or (-max_f2 <= x <= max_f2)
elif dtype.kind == u'f' and dtype.itemsize == 4:
max_f4 = (2. - 2 ** -23) * 2 ** 127
self.check_cast = lambda x: \
(not np.isfinite(x)) or (-max_f4 <= x <= max_f4)
else:
self.check_cast = lambda x: True

If possible we should also check for truncation in string arrays.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
legibility make errors helpful and Hypothesis grokable
Projects
None yet
Development

No branches or pull requests

1 participant