why is numpy prod not working as intended?

Question:

I have a list with n elements and I need to know the product between them all, so:
[x1, x2, x3, …] and I need the result of x1 * x2 * x3 …

So i tried the numpy funcion prod as follows:

np.array([20, 19, 18, 17, 16, 15, 14]).prod()

which works fine, like it should. But, when i add the element 13 to the list, the code returns the incorrect answer. The following code gives the answer of 784,143,104

np.array([20, 19, 18, 17, 16, 15, 14, 13]).prod()

, while this gives the correct answer, which is 5,079,110,400

20 * 19 * 18 * 17 * 16 * 15 * 14 * 13

The curious thing is that when I remove the element 13, both give the same correct answer, which is 390,700,800.
What am I doing wrong here?

Asked By: Gustavo

||

Answers:

By default, NumPy determines the data type based on the provided elements. In your case, the elements of the list are integers, and when you add the number 13, the data type is changed to int64, which has limited storage capacity

import numpy as np

arr = np.array([20, 19, 18, 17, 16, 15, 14, 13], dtype=np.int128)
result = arr.prod()

print(result)
Answered By: Hebert Ribeiro

Due to integer overflow. The result of multiplying the elements of the list exceeds the maximum value that can be represented by the data type you are using.

To overcome this issue, you can consider using a data type that can handle larger numbers, such as numpy.int64

import numpy as np

result = np.array([20, 19, 18, 17, 16, 15, 14, 13], dtype=np.int64).prod()
Answered By: xFranko
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.