简体   繁体   English

为什么从 numpy 按位左移在不同系统上会给出不同的结果?

[英]Why does bitwise left shift from numpy give different results on different systems?

I use bitwise operations from numpy on two different system and get different / inconsistent results.我在两个不同的系统上使用 numpy 的按位运算并得到不同/不一致的结果。 After a bit of investigation I found out that left_shift causes the problem.经过一番调查,我发现left_shift导致了问题。 If I do the following:如果我执行以下操作:

xs = [i for i in range(100)]
np.left_shift(xs, xs)

On the first system I get:在我得到的第一个系统上:

array([                   0,                    2,                    8,
                         24,                   64,                  160,
                        384,                  896,                 2048,
                       4608,                10240,                22528,
                      49152,               106496,               229376,
                     491520,              1048576,              2228224,
                    4718592,              9961472,             20971520,
                   44040192,             92274688,            192937984,
                  402653184,            838860800,           1744830464,
                 3623878656,           7516192768,          15569256448,
                32212254720,          66571993088,         137438953472,
               283467841536,         584115552256,        1202590842880,
              2473901162496,        5085241278464,       10445360463872,
             21440476741632,       43980465111040,       90159953477632,
            184717953466368,      378231999954944,      774056185954304,
           1583296743997440,     3236962232172544,     6614661952700416,
          13510798882111488,    27584547717644288,    56294995342131200,
         114841790497947648,   234187180623265792,   477381560501272576,
         972777519512027136,  1981583836043018240,  4035225266123964416,
        8214565720323784704, -1729382256910270464, -2882303761517117440,
       -4611686018427387904, -6917529027641081856, -9223372036854775808,
       -9223372036854775808,                   64,                  130,
                        264,                  536,                 1088,
                       2208,                 4480,                 9088,
                      18432,                37376,                75776,
                     153600,               311296,               630784,
                    1277952,              2588672,              5242880,
                   10616832,             21495808,             43515904,
                   88080384,            178257920,            360710144,
                  729808896,           1476395008,           2986344448,
                 6039797760,          12213813248,          24696061952,
                49928994816,         100931731456,         204010946560,
               412316860416,         833223655424,        1683627180032,
              3401614098432])

And on the second system I get:在第二个系统上,我得到:

array([                   0,                    2,                    8,
                         24,                   64,                  160,
                        384,                  896,                 2048,
                       4608,                10240,                22528,
                      49152,               106496,               229376,
                     491520,              1048576,              2228224,
                    4718592,              9961472,             20971520,
                   44040192,             92274688,            192937984,
                  402653184,            838860800,           1744830464,
                 3623878656,           7516192768,          15569256448,
                32212254720,          66571993088,         137438953472,
               283467841536,         584115552256,        1202590842880,
              2473901162496,        5085241278464,       10445360463872,
             21440476741632,       43980465111040,       90159953477632,
            184717953466368,      378231999954944,      774056185954304,
           1583296743997440,     3236962232172544,     6614661952700416,
          13510798882111488,    27584547717644288,    56294995342131200,
         114841790497947648,   234187180623265792,   477381560501272576,
         972777519512027136,  1981583836043018240,  4035225266123964416,
        8214565720323784704, -1729382256910270464, -2882303761517117440,
       -4611686018427387904, -6917529027641081856, -9223372036854775808,
       -9223372036854775808,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0])

So, as you can see, up to a certain points the results become inconsistent.因此,如您所见,在某些情况下,结果会变得不一致。 Does any one know what could cause it?有谁知道是什么原因造成的?

ADDED添加

By "different systems" I mean different computers. “不同的系统”是指不同的计算机。 The first computer has numpy 1.15.1 and the second one 1.15.2.第一台计算机具有 numpy 1.15.1,第二台计算机具有 1.15.2。

Old versions of NumPy don't define what shift operators do if you try to shift by an amount >= the width of a type.旧版本的 NumPy 没有定义移位运算符在尝试移位 >= 类型宽度时的操作。 They basically just delegate the semantics to the C shift operators, for which this is undefined behavior .他们基本上只是将语义委托给 C 移位运算符,对此未定义行为 Depending on hardware and compiler details, pretty much anything could happen (theoretically up to and including crashes, security issues, and arbitrary other misbehavior).根据硬件和编译器细节,几乎任何事情都可能发生(理论上包括崩溃、安全问题和任意其他不当行为)。

This got changed in 1.18, but your NumPy versions are older than that.这在 1.18 中有所更改,但您的 NumPy 版本比这更旧。

If you want defined behavior, don't try to do shifts like that.如果您想要定义的行为,请不要尝试进行这样的转变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按位左移相同操作数的不同结果 - bitwise left shift different results for identical operands 为什么逐位移位会在Python和Java中返回不同的结果? - Why does bit-wise shift left return different results in Python and Java? 为什么myHDL手册中的示例为我提供了不同的结果? - Why does this example from the myHDL manual give me different results? 为什么我自己的 N 维快速傅立叶变换实现与 NumPy 的结果不同? - Why does my own implementation of Fast Fourier Transform in N dimensions give different results than NumPy's? 为什么 numpy RandomState 会给出不同的结果? - Why does numpy RandomState gives different results? 为什么这两个 numpy.divide 操作会给出如此不同的结果? - Why do these two numpy.divide operations give such different results? 为什么numpy std()与matlab std()给出不同的结果? - Why does numpy std() give a different result to matlab std()? 为什么scipy.stats.nanmean与numpy.nansum给出不同的结果? - Why does scipy.stats.nanmean give different result from numpy.nansum? Matlab和numpy / scipy中的FFT给出不同的结果 - FFT in Matlab and numpy / scipy give different results matlab和numpy中的ifft给出不同的结果 - ifft in matlab and numpy give different results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM