diff options
author | Warren Weckesser <warren.weckesser@gmail.com> | 2019-08-18 12:07:55 -0400 |
---|---|---|
committer | Warren Weckesser <warren.weckesser@gmail.com> | 2019-08-18 12:59:41 -0400 |
commit | dd9051c78476be2bc2cbf03ab895e8dde5ca14ca (patch) | |
tree | d5e0b99fffac5e28c2ad8d96344414d386d14dd4 /numpy | |
parent | a6729a0e43791069cbc3cf5e80c34c73fa752b4d (diff) | |
download | numpy-dd9051c78476be2bc2cbf03ab895e8dde5ca14ca.tar.gz |
BUG: core: Handle large negative np.int64 args in binary_repr.
To avoid the cast to floating point that can occur when the first
argument to binary_repr is a NumPy integer, the argument is converted
to a Python integer at the beginning of the function.
Closes gh-14289.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/numeric.py | 4 | ||||
-rw-r--r-- | numpy/core/tests/test_numeric.py | 5 |
2 files changed, 9 insertions, 0 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index ff8c58867..bbcd58abb 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1935,6 +1935,10 @@ def binary_repr(num, width=None): "will raise an error in the future.", DeprecationWarning, stacklevel=3) + # Ensure that num is a Python integer to avoid overflow or unwanted + # casts to floating point. + num = operator.index(num) + if num == 0: return '0' * (width or 1) diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 3e85054b7..c479a0f6d 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1341,6 +1341,11 @@ class TestBinaryRepr(object): exp = '1' + (width - 1) * '0' assert_equal(np.binary_repr(num, width=width), exp) + def test_large_neg_int64(self): + # See gh-14289. + assert_equal(np.binary_repr(np.int64(-2**62), width=64), + '11' + '0'*62) + class TestBaseRepr(object): def test_base3(self): |