[Numpy-discussion] numpy log2 has bug

Robert Kern robert.kern at gmail.com
Wed Mar 23 15:46:32 EDT 2011

On Wed, Mar 23, 2011 at 13:51,  <josef.pktd at gmail.com> wrote:
> 2011/3/23 Dmitrey <tmp50 at ukr.net>:
>>>>> from numpy import log2, __version__
>>>>> log2(2**63)
>> Traceback (most recent call
>> last):
>>   File "<stdin>", line 1, in
>> <module>
>> AttributeError: log2
>>>>> __version__
>> '2.0.0.dev-1fe8136'
>> (doesn't work with 1.3.0 as well)
>>>> np.array([2**63])
> array([9223372036854775808], dtype=object)
>>>> log2(2.**63)
> 62.999999999999993
>>>> log2(2**63)
> Traceback (most recent call last):
>  File "<pyshell#9>", line 1, in <module>
>    log2(2**63)
> AttributeError: log2
> integer conversion problem

Right. numpy cannot safely convert a long object of that size to a
dtype it knows about, so it leaves it as an object array. Most ufuncs
operate on object arrays by looking for a method on each element with
the name of the ufunc. So

  np.log2(np.array([x], dtype=object))

will look for x.log2().

Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco

More information about the NumPy-Discussion mailing list