summaryrefslogtreecommitdiff
path: root/numpy/lib/arraysetops.py
diff options
context:
space:
mode:
authorMilesCranmer <miles.cranmer@gmail.com>2018-12-26 18:57:25 -0500
committerMilesCranmer <miles.cranmer@gmail.com>2022-06-09 20:37:24 -0400
commit0f6108c2b37a70571bee6185dbe0ffa92adf995e (patch)
treedcd9bde9dd0d9ffa734392624e2717381fc26c0b /numpy/lib/arraysetops.py
parent935e3d91822c436d519b7120820d4295eaa95fbf (diff)
downloadnumpy-0f6108c2b37a70571bee6185dbe0ffa92adf995e.tar.gz
MAINT: Check for overflow in integral np.isin
- Before this fix, if, e.g., int8(-100) and int8(100) are the min and max, respectively, of ar2, ar2_range would experience an integer overflow and optimal_parameters would be an invalid value. ``if optimal_parameters:`` would skip the fast integer algorithm regardless, but warnings would be generated when they are unnecessary. - np.log10(... + 1) was changed to np.log10(... + 1.0) to avoid further potential problems from overflow here. - See https://github.com/numpy/numpy/pull/12065 for discussion.
Diffstat (limited to 'numpy/lib/arraysetops.py')
-rw-r--r--numpy/lib/arraysetops.py25
1 files changed, 16 insertions, 9 deletions
diff --git a/numpy/lib/arraysetops.py b/numpy/lib/arraysetops.py
index a0ee8d9b2..8ae00aeae 100644
--- a/numpy/lib/arraysetops.py
+++ b/numpy/lib/arraysetops.py
@@ -606,17 +606,24 @@ def in1d(ar1, ar2, assume_unique=False, invert=False):
if integer_arrays:
ar2_min = np.min(ar2)
ar2_max = np.max(ar2)
- ar2_range = ar2_max - ar2_min
ar2_size = ar2.size
- # Optimal performance is for approximately
- # log10(size) > (log10(range) - 2.27) / 0.927, see discussion on
- # https://github.com/numpy/numpy/pull/12065
- optimal_parameters = (
- np.log10(ar2_size + 1) >
- ((np.log10(ar2_range + 1) - 2.27) / 0.927)
- )
-
+ # Check for integer overflow
+ with np.errstate(over='raise'):
+ try:
+ ar2_range = ar2_max - ar2_min
+
+ # Optimal performance is for approximately
+ # log10(size) > (log10(range) - 2.27) / 0.927, see discussion on
+ # https://github.com/numpy/numpy/pull/12065
+ optimal_parameters = (
+ np.log10(ar2_size) >
+ ((np.log10(ar2_range + 1.0) - 2.27) / 0.927)
+ )
+ except FloatingPointError:
+ optimal_parameters = False
+
+ # Use the fast integer algorithm
if optimal_parameters:
if invert: