2

I tried :

>>>a = np.array(2**np.arange(42)[::-1],dtype = np.uint64)

and got

>>> a
array([                   0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0, 18446744071562067968,           1073741824,
                  536870912,            268435456,            134217728,
                   67108864,             33554432,             16777216,
                    8388608,              4194304,              2097152,
                    1048576,               524288,               262144,
                     131072,                65536,                32768,
                      16384,                 8192,                 4096,
                       2048,                 1024,                  512,
                        256,                  128,                   64,
                         32,                   16,                    8,
                          4,                    2,                    1], dtype=uint64)

18446744071562067968 is certainly not 2 to the power 31 and after that the answers are certainly not zero!
to check for some kind of overflow error I tried :

>>> a[0] = 2**42

and got:

>>> a
array([       4398046511104, ..... 

  the rest the same  

Can anybody tell me what has gone wrong?

1
  • You're doing the computations in the wrong dtype and casting too late. Same reason float(1/2) is 0.0 in Python 2 and decimal.Decimal(1.0/10.0) isn't decimal.Decimal('0.1'). Commented Oct 9, 2017 at 19:35

1 Answer 1

3

Yes, np.arange(42) is defaulting to a signed integer, likely np.int32, probably because you are on a 32-bit architecture or you are on Windows. Ditch the np.array wrapper, which is totally useless, and pass the dtype to np.arange:

2**np.arange(42, dtype = np.uint64)[::-1]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.