[Numpy-discussion] Problems with long

Tom Johnson tjhnson@gmail....
Wed Mar 12 05:10:13 CDT 2008


On Sat, Jan 26, 2008 at 5:49 AM, Travis E. Oliphant
<oliphant@enthought.com> wrote:
>
> Tom Johnson wrote:
>  > Hi, I'm having some troubles with long.
>  >
>  >
>  >>>> from numpy import log
>  >>>> log(8463186938969424928L)
>  >>>>
>  > 43.5822574833
>  >
>  >>>> log(10454852688145851272L)
>  >>>>
>  > <type 'exceptions.AttributeError'>: 'long' object has no attribute 'log'
>  >
>
>  The problem is that the latter long integer is too big to fit into an
>  int64 (long long) and so it converts it to an object array.  The default
>  behavior of log on object arrays is to look for a method on each element
>  of the array called log and call that.
>
>  Your best bet is to convert to double before calling log
>
>  log(float(10454852688145851272L))
>
>  -Travis O.
>

Related, I understand that problem which occurs below...

>>> x = 8463186938969424928L
>>> y = 10454852688145851272L
>>> import numpy
>>> z = numpy.float_(3)
>>> x * z
 2.53895608169e+19
>>> y * z
TypeError: unsupported operand type(s) for *: 'long' and 'numpy.float64'
>>> numpy.float_(y) * z
3.13645580644e+19
>>> y * float(z)
3.1364558064437551e+19

A couple points....

1) With log, we get an AttributeError...with multiplication, we get a
TypeError.  I know the mechanism which causes the problem is different
but the fundamental problem (too large of longs) is the same in both
cases.  Can this be improved upon?

2) The extra digits from python floats are nice....can numpy have these as well?

3) I think it is safe to say that many people cannot know ahead of
time if their longs will be larger than 64-bit.  This whole situation
seems unstable to me...code that seems to be working will work, and
then when the longs (from python) get too large we get a variety of
different exceptions.

So, I wonder aloud:  Is this being handled is the nicest/preferred way?

I'd be happy if my extremely large longs were automatically converted
to numpy.float64_....even if we don't have as many significant digits
as the equivalent pure python result.  At least with this method, I
will not have code "randomly" breaking.  Either that, or am I required
to be extremely careful about mixing types.


More information about the Numpy-discussion mailing list