[NumPy-Tickets] [NumPy] #1629: np.prod returns 0 in case of integer overflow
NumPy Trac
numpy-tickets@scipy....
Sun Oct 10 00:18:36 CDT 2010
#1629: np.prod returns 0 in case of integer overflow
--------------------+-------------------------------------------------------
Reporter: kilian | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone: Unscheduled
Component: Other | Version: 1.4.0
Keywords: |
--------------------+-------------------------------------------------------
Comment(by rgommers):
It does depend on whether the platform is 32/64-bit:
{{{
In [10]: np.prod([2**16,2**16])
Out[10]: 0
In [11]: np.prod([2**16,2**16]).dtype
Out[11]: dtype('int32')
}}}
But the documentation explains this issue in detail, quoting relevant
parts:
{{{
Parameters
----------
...
dtype : data-type, optional
The data-type of the returned array, as well as of the accumulator
in which the elements are multiplied. By default, if `a` is of
integer type, `dtype` is the default platform integer. (Note: if
the type of `a` is unsigned, then so is `dtype`.) Otherwise,
the dtype is the same as that of `a`.
...
Notes
-----
Arithmetic is modular when using integer types, and no error is
raised on overflow. That means that, on a 32-bit platform:
>>> x = np.array([536870910, 536870910, 536870910, 536870910])
>>> np.prod(x) #random
16
}}}
There probably was a good reason to implement and document it like this,
but that's best asked on the mailing list. I think this ticket can be
closed.
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1629#comment:2>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list