簡體   English   中英

Python:到 Integer 的位數組:使用 NumPy ZA3CCBC4F9D0CE2DE7BD155 隨機獲取負整數

[英]Python: Bit Array to Integer: Randomly getting negative integers with NumPy arrays

Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
>>> np.version.version
'1.17.2'

我將整數維護為位數組,以便在位級算法上進行操作。 我正在使用一種相當標准的方式將位數組轉換為 integer,然后以十進制格式查看。

def decimal(binaryValue):
    decimalValue = 0
    for bit in binaryValue:
        decimalValue = (decimalValue << 1) | bit
        print(decimalValue) #For testing
    return decimalValue

對於某些 arrays 64 位或更大版本,我奇怪地隨機接收負整數。 經過一番拉扯和瘋狂調試后,我意識到當我使用 NumPy 陣列時會發生這種情況。 常規列表沒有問題。 這是一個具體的例子:

>>> b = [1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1]
>>> decimal(b)
1
3
7
...
4418570559336253839
8837141118672507678
17674282237345015357
17674282237345015357
>>> import numpy as np
>>> b_np = np.array(b)
>>> b_np
array([1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0,
   1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
   0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1])
>>> decimal(b_np)
1
3
7
...
4418570559336253839
8837141118672507678
-772461836364536259
-772461836364536259
>>> np.binary_repr(decimal(b))
'1111010101000111101010100000110101110000111100100010011000111101'
>>> np.binary_repr(decimal(b_np))
'-101010111000010101011111001010001111000011011101100111000011'

如您所見,使用 numpy 數組表示,在最后一位求值時會發生一些事情。 如果我將 numpy 數組轉換回列表,那么我現在得到一個負數。 非常非常奇怪。 numpy 空間發生了一些事情。 但是 c 與 b 相同。

>>> c = list(b_np)
>>> c
[1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1]
>>> decimal(c)
1
3
7
...
4418570559336253839
8837141118672507678
-772461836364536259
-772461836364536259
>>>np_binary_rep(decimal(c))
'-101010111000010101011111001010001111000011011101100111000011'

簡單檢查:

>>> len(b)
64
>>> len(b_np)
64
>>> len(c)
64
>>> b == c
True
>>> b == b_np
array([ True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True,  True,  True,  True,  True,  True,  True,  True,  True,
        True])

到底是怎么回事? 顯然是64位問題,但看不到在哪里。 謝謝。

當您嘗試轉換b變量時,您正在將int數據類型分配給decimal變量,但是當您使用numpy.array數據類型(如b_np )執行此操作時,您正在分配numpy.int64數據類型。

int

int 類型可以表示 integer 高達sys.maxsize * 2 + 1

import sys
print(sys.maxsize * 2 + 1)
# Prints 18446744073709551615 (length 20)

這意味着您的第一個結果17674282237345015357可以用無符號整數表示,因為18446744073709551615 > 17674282237345015357 == True

numpy.int64 type

此數據類型可以表示-9223372036854775808 to 9223372036854775807之間的整數,您可以在此處查看
由於9223372036854775807 (長度 19)小於您的預期結果18446744073709551615 ,因此第 64 個字節被視為符號,這就是為什么只有您的最后一個結果給出負值的原因。 如果您嘗試使用具有 '62 length ( 2**62' 可表示值)的數組的相同示例,您會發現不會有負值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM