0

I identified a strange behaviour of the ** operator. Here is the code:

import numpy as np

n = np.arange(0,20)

print(np.c_[n,10 **n])

I obtain the following output:

array([[                   0,                    1],
       [                   1,                   10],
       [                   2,                  100],
       [                   3,                 1000],
       [                   4,                10000],
       [                   5,               100000],
       [                   6,              1000000],
       [                   7,             10000000],
       [                   8,            100000000],
       [                   9,           1000000000],
       [                  10,          10000000000],
       [                  11,         100000000000],
       [                  12,        1000000000000],
       [                  13,       10000000000000],
       [                  14,      100000000000000],
       [                  15,     1000000000000000],
       [                  16,    10000000000000000],
       [                  17,   100000000000000000],
       [                  18,  1000000000000000000],
       [                  19, -8446744073709551616]])

I do not understand the reason of the out for n=19.

2

1 Answer 1

0

as mentioned by @Ankur Ankan, this is an overflow error, you can check numpy data types to see the range of each type. For example change the dtype to np.uint64 will cover the whole array

n = np.arange(0,20).astype(np.uint64)
print(np.c_[n,10**n])
[[                   0                    1]
 [                   1                   10]
 [                   2                  100]
 [                   3                 1000]
 [                   4                10000]
 [                   5               100000]
 [                   6              1000000]
 [                   7             10000000]
 [                   8            100000000]
 [                   9           1000000000]
 [                  10          10000000000]
 [                  11         100000000000]
 [                  12        1000000000000]
 [                  13       10000000000000]
 [                  14      100000000000000]
 [                  15     1000000000000000]
 [                  16    10000000000000000]
 [                  17   100000000000000000]
 [                  18  1000000000000000000]
 [                  19 10000000000000000000]]
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.