diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2017-02-23 11:57:41 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-23 11:57:41 -0700 |
commit | e0577251672dd2ceb905a6589beb1bf6825f98fc (patch) | |
tree | e9760976a83b89e0e5c5f6ffa7130c5729d31cd9 /numpy/core/numeric.py | |
parent | ddf8d9d8b6c521df8cba9edb3a101e9686c684af (diff) | |
parent | b10b4087132d51ee33bf5e0777282af23d6ee3cd (diff) | |
download | numpy-e0577251672dd2ceb905a6589beb1bf6825f98fc.tar.gz |
Merge pull request #8674 from gfyoung/binary-repr-boundary
BUG: Remove extra digit in binary_repr at limit
Diffstat (limited to 'numpy/core/numeric.py')
-rw-r--r-- | numpy/core/numeric.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 066697f3e..d4d4045a0 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2206,7 +2206,7 @@ def binary_repr(num, width=None): designated form. If the `width` value is insufficient, it will be ignored, and `num` will - be returned in binary(`num` > 0) or two's complement (`num` < 0) form + be returned in binary (`num` > 0) or two's complement (`num` < 0) form with its width equal to the minimum number of bits needed to represent the number in the designated form. This behavior is deprecated and will later raise an error. @@ -2276,10 +2276,16 @@ def binary_repr(num, width=None): else: poswidth = len(bin(-num)[2:]) - twocomp = 2**(poswidth + 1) + num + # See gh-8679: remove extra digit + # for numbers at boundaries. + if 2**(poswidth - 1) == -num: + poswidth -= 1 + + twocomp = 2**(poswidth + 1) + num binary = bin(twocomp)[2:] binwidth = len(binary) + outwidth = max(binwidth, width) warn_if_insufficient(width, binwidth) return '1' * (outwidth - binwidth) + binary |